我遇到了一些語法問題。我對接口並不熟悉,請原諒我的無知。「使用未分配的本地變量」錯誤與接口
VS2010是給我一個錯誤的...... application.Name = System.AppDomain.CurrentDomain.FriendlyName;
public static void AddApplication(string applicationName = null, string processImageFileName = null)
{
INetFwAuthorizedApplications applications;
INetFwAuthorizedApplication application;
if(applicationName == null)
{
application.Name = System.AppDomain.CurrentDomain.FriendlyName;/*set the name of the application */
}
else
{
application.Name = applicationName;/*set the name of the application */
}
if (processImageFileName == null)
{
application.ProcessImageFileName = System.Reflection.Assembly.GetExecutingAssembly().Location; /* set this property to the location of the executable file of the application*/
}
else
{
application.ProcessImageFileName = processImageFileName; /* set this property to the location of the executable file of the application*/
}
application.Enabled = true; //enable it
/*now add this application to AuthorizedApplications collection */
Type NetFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false);
INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(NetFwMgrType);
applications = (INetFwAuthorizedApplications)mgr.LocalPolicy.CurrentProfile.AuthorizedApplications;
applications.Add(application);
}
我可以作出這樣的錯誤設置application
到null
走開但導致運行時間空引用錯誤。
編輯:
這裏就是我適應從代碼。我希望它讓更多的上下文 http://blogs.msdn.com/b/securitytools/archive/2009/08/21/automating-windows-firewall-settings-with-c.aspx
錯誤說明究竟是什麼錯誤。你聲明瞭一個變量並且從不初始化它。 – Wug 2012-07-09 20:05:47
在你引用的代碼中,'applications'初始化爲'(INetFwAuthorizedApplications)mgr.LocalPolicy.CurrentProfile.AuthorizedApplications;'然後通過將每個條目分配給'application'來循環這個集合。你沒有這樣做。 – comecme 2012-07-09 20:16:06