2

いくつかの構文に問題があります。私はインターフェイスにあまり詳しくないので、無知を許してください。

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が、実行時の null 参照エラーが発生します。

編集:

ここからコードを適応させます。より多くのコンテキストが得られることを願っています http://blogs.msdn.com/b/securitytools/archive/2009/08/21/automating-windows-firewall-settings-with-c.aspx

4

2 に答える 2

9

あなたは決して初期化しない

application

ここで使用する前に:

application.Name = System.AppDomain.CurrentDomain.FriendlyName;

変数 application は次のように定義されます。

INetFwAuthorizedApplication application

インターフェイスを実装するクラスのインスタンスを割り当てる必要がありますINetFwAuthorizedApplication

プロジェクトのどこかに、次のようなクラスが 1 つ (またはおそらく複数) あるはずです。

public class SomeClass : INetFwAuthorizedApplication
{
    // ...
}

public class AnotherClass : INetFwAuthorizedApplication
{
    // ...
}

使用するクラス (SomeClass、AnotherClass) を決定してから、次のように適切なオブジェクトを割り当てる必要があります。

INetFwAuthorizedApplication application = new SomeClass();
于 2012-07-09T20:05:14.820 に答える
1

インターフェイスは、オブジェクトが具体的に何をするかではなく、オブジェクトが何をするかを説明するために使用されます。「現実世界」の用語で言えば、インターフェースは次のようになります。

ISmallerThanABreadboxFitIntoBreadbox()メソッドで。「ブレッドボックスよりも小さいもの」をくださいと頼むことはできません...意味がないからです。「ブレッドボックスよりも小さい」ものをくださいとお願いすることしかできません。インターフェースを持つのに適した独自のオブジェクトを考え出す必要があります。リンゴはブレッドボックスよりも小さいため、ブレッドボックスよりも小さいアイテムのみを保持するブレッドボックスがある場合、リンゴはISmallerThanABreadboxインターフェイスの適切な候補です。

もう 1 つの例はIGraspableHold()メソッドとFitsInPocketbool プロパティを使用したものです。ポケットに収まるかもしれないし、収まらないかもしれない何かが掴めるように頼むことはできますが、「掴めるもの」を求めることはできません。

それが役立つことを願っています...

于 2012-07-09T20:17:14.560 に答える