Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过编程告诉windows防火墙使用专用网络_C#_Windows Firewall - Fatal编程技术网

C# 通过编程告诉windows防火墙使用专用网络

C# 通过编程告诉windows防火墙使用专用网络,c#,windows-firewall,C#,Windows Firewall,当我运行我的c#控制台应用程序时,windows防火墙会弹出,请求访问vshost32(我的应用程序使用TCP和UDP监听端口1234上的输入消息)。我接受建议(私人网络)。然后,控制台应用程序可以正常工作 我不想让用户处理这个问题,所以我添加了下面的代码。然而,当我在“控制面板”>“防火墙”中调查这项功能时,它似乎是为“公共网络”而不是专用网络启用的。这是没有用的,因为允许我的应用程序工作 下面的代码中是否有调整,以将其强制到专用网络 INetFwOpenPorts ports3; INetF

当我运行我的c#控制台应用程序时,windows防火墙会弹出,请求访问vshost32(我的应用程序使用TCP和UDP监听端口1234上的输入消息)。我接受建议(私人网络)。然后,控制台应用程序可以正常工作

我不想让用户处理这个问题,所以我添加了下面的代码。然而,当我在“控制面板”>“防火墙”中调查这项功能时,它似乎是为“公共网络”而不是专用网络启用的。这是没有用的,因为允许我的应用程序工作

下面的代码中是否有调整,以将其强制到专用网络

INetFwOpenPorts ports3;
INetFwOpenPort port3 = (INetFwOpenPort)Activator.CreateInstance(
    Type.GetTypeFromProgID("HNetCfg.FWOpenPort"));
port3.Port = 1234; 
port3.Name = "vshost32.exe"; 
port3.Enabled = true;

//**UPDATE** added for suggestion in answer below - still doesnt change anything though
port3.Scope = NetFwTypeLib.NET_FW_SCOPE_.NET_FW_SCOPE_LOCAL_SUBNET;

Type NetFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false);
INetFwMgr mgr3 = (INetFwMgr)Activator.CreateInstance(NetFwMgrType);
ports3 = (INetFwOpenPorts)mgr3.LocalPolicy.CurrentProfile.GloballyOpenPorts;
ports3.Add(port3);
请参阅我的上一篇文章

请看下面几行:

private static int Main (string [] args)
{
    var application = new NetFwAuthorizedApplication()
    {
        Name = "MyService",
        Enabled = true,
        RemoteAddresses = "*",
        Scope = NET_FW_SCOPE_.NET_FW_SCOPE_ALL,
        IpVersion = NET_FW_IP_VERSION_.NET_FW_IP_VERSION_ANY,
        ProcessImageFileName = "ServiceAssemblyName.dll",
    };

    return (FirewallUtilities.AddApplication(application, out exception) ? 0 : -1);
}
NET_FW_SCOPE_uu枚举具有以下值:

  • NET_FW_SCOPE_ALL=0
  • NET_FW_SCOPE_LOCAL_SUBNET=1
  • NET_FW_SCOPE_CUSTOM=2
  • 净范围最大值=3
您可以进一步将端口、协议以及远程地址限制为该规则

更新:

下面是缺少的
ReleaseComObject
函数。将其放置在任何名称空间中,并删除对
ComUtilities
的引用

    public static void ReleaseComObject (object o)
    {
        try
        {
            if (o != null)
            {
                if (Marshal.IsComObject(o))
                {
                    Marshal.ReleaseComObject(o);
                }
            }
        }
        finally
        {
            o = null;
        }
    }
以下是
NetFwAuthorizedApplication
类:

命名空间MySolution.Configurator.Firewall { 使用制度; 使用System.Linq; 使用NetFwTypeLib

public sealed class NetFwAuthorizedApplication:
    INetFwAuthorizedApplication
{
    public string Name { get; set; }
    public bool Enabled { get; set; }
    public NET_FW_SCOPE_ Scope { get; set; }
    public string RemoteAddresses { get; set; }
    public string ProcessImageFileName { get; set; }
    public NET_FW_IP_VERSION_ IpVersion { get; set; }

    public NetFwAuthorizedApplication ()
    {
        this.Name = "";
        this.Enabled = false;
        this.RemoteAddresses = "";
        this.ProcessImageFileName = "";
        this.Scope = NET_FW_SCOPE_.NET_FW_SCOPE_ALL;
        this.IpVersion = NET_FW_IP_VERSION_.NET_FW_IP_VERSION_ANY;
    }

    public NetFwAuthorizedApplication (string name, bool enabled, string remoteAddresses, NET_FW_SCOPE_ scope, NET_FW_IP_VERSION_ ipVersion, string processImageFileName)
    {
        this.Name = name;
        this.Scope = scope;
        this.Enabled = enabled;
        this.IpVersion = ipVersion;
        this.RemoteAddresses = remoteAddresses;
        this.ProcessImageFileName = processImageFileName;
    }

    public static NetFwAuthorizedApplication FromINetFwAuthorizedApplication (INetFwAuthorizedApplication application)
    {
        return (new NetFwAuthorizedApplication(application.Name, application.Enabled, application.RemoteAddresses, application.Scope, application.IpVersion, application.ProcessImageFileName));
    }
}
}

INetFwRule firewallRule=(INetFwRule)Activator.CreateInstance(
Type.GetTypeFromProgID(“HNetCfg.FWRule”);
INetFwPolicy2 firewallPolicy=(INetFwPolicy2)Activator.CreateInstance(
Type.GetTypeFromProgID(“HNetCfg.FwPolicy2”);
firewallRule.ApplicationName=“”;
firewallRule.Action=NET\u FW\u Action\u.NET\u FW\u Action\u ALLOW;
firewallRule.Description=“我的Windows防火墙规则”;
firewallRule.Enabled=true;
firewallRule.InterfaceTypes=“全部”;
firewallRule.Name=“”;
//应该在加载项之前检查规则是否不存在
firewallPolicy.Rules.Add(firewallRule);

谢谢,我猜NET\u FW\u SCOPE\u LOCAL\u SUBNET等同于“private”。奇怪的是,我将它添加到了(请参阅有问题的添加行)
port3.SCOPE=netfwtypelb.NET\u FW\u SCOPE\u.NET\u FW\u SCOPE\u LOCAL\u SUBNETNET\u FW\u SCOPE\u ALL
。似乎您正在操纵全局开放端口集合。如果您的windows防火墙设置配置为通用默认设置,我不确定这是否允许访问特定的应用程序。看看这个答案中的AddApplication方法。它将添加到授权应用程序集合中。顺便说一下,您可以“按原样”使用这两个类。只需将
Main
方法内容粘贴到您的控制台应用程序中,您就可以开始了。我尝试了您的解决方案,但我不知道如何解决
ComUtilities
NetFwAuthorizedApplication
。我试图在nuget上找到一个包裹。老实说,我有点不知所措的代码量,并没有信心调整它。任何关于如何开始跑步的提示,我都很感激。我的错。
NetFwAuthorizedApplication
只是一个实现
INetFwAuthorizedApplication
的自定义类。它出现在那个答案中,但我必须替换名称空间。在上面的答案中还包括
com实用程序
。不要担心代码的数量。这两个类是完全独立的。几分钟后再回来查看答案的更新。
    INetFwRule firewallRule = (INetFwRule)Activator.CreateInstance(
        Type.GetTypeFromProgID("HNetCfg.FWRule"));

    INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(
        Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));

    firewallRule.ApplicationName = "<path to your app>";

    firewallRule.Action = NET_FW_ACTION_.NET_FW_ACTION_ALLOW;
    firewallRule.Description = " My Windows Firewall Rule";
    firewallRule.Enabled = true;
    firewallRule.InterfaceTypes = "All";
    firewallRule.Name = "<your rule name>";

    // Should really check that rule is not already present before add in
    firewallPolicy.Rules.Add(firewallRule);