c# 添加防火墙例外端口,如何添加出站Windows防火墙例外?

I need to open up the Windows Firewall for outbound connections for an application I'm writing.

The best answers I've been able to locate are here:

The problem is that method only creates an inbound rule, and not an outbound rule. (Both the C# and InnoSetup script use the same method.) This is entirely useless for me.

The default behaviour for the Windows Firewall is to allow outbound traffic, but that doesn't guarantee that someone won't change that.

I would prefer to do this in the installer (using InnoSetup) rather than doing it in C#.

Did I miss something?

Does anyone know how to create an outbound rule?

解决方案

You can use netsh if you need add some exceptions for your application.

write in command line (for XP):

netsh firewall add allowedprogram ?

write in command line (for W7):

netsh advfirewall firewall add rule ?

This difference becouse netsh firewall command is deprecated. Instead, we have to use the command netsh advfirewall firewall.

More information about using the command netsh advfirewall firewall instead of the netsh firewall command we can see in Knowledge Base there: http://go.microsoft.com/fwlink/?linkid=121488

Examples:

Adding a rule for incoming traffic without security encapsulation for messenger.exe:

netsh advfirewall firewall add rule name="allow messenger" dir=in program="c:\programfiles\messenger\msmsgs.exe" security=authnoencap action=allow

Adding a rule for outgoing traffic at the port 80:

netsh advfirewall firewall add rule name="allow80" protocol=TCP dir=out localport=80 action=block

Adding rules to inbound traffic with safety & traffic encryption for TCP through port 80:

netsh advfirewall firewall add rule name="Require Encryption for Inbound TCP/80" protocol=TCP dir=in localport=80 security=authdynenc action=allow

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用C#中的System.Management命名空间中的ManagementObject类来获取防火墙状态和修改和添加防火墙规则。以下是获取防火墙状态的示例代码: ```csharp using System.Management; public static bool IsFirewallEnabled() { bool isEnabled = false; ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM CIM_Service WHERE DisplayName = 'Windows Firewall'"); foreach (ManagementObject obj in searcher.Get()) { isEnabled = (bool)obj["Started"]; break; } return isEnabled; } ``` 以下是添加防火墙规则的示例代码: ```csharp using System.Management; public static void AddFirewallRule(string ruleName, string ruleDescription, string applicationPath, bool isEnable = true) { ManagementObject classInstance = new ManagementObject("root\\StandardCimv2", "MSFT_NetFirewallRule", null); //set properties for the new rule classInstance.SetPropertyValue("Name", ruleName); classInstance.SetPropertyValue("Description", ruleDescription); classInstance.SetPropertyValue("ApplicationPath", applicationPath); classInstance.SetPropertyValue("Enabled", isEnable); classInstance.SetPropertyValue("Direction", 1); // 1 = Inbound, 2 = Outbound classInstance.SetPropertyValue("Profile", 0x7fffffff); // All profiles //invoke the Create method to create the new rule ManagementBaseObject inParams = classInstance.GetMethodParameters("Create"); ManagementBaseObject outParams = classInstance.InvokeMethod("Create", inParams, null); } ``` 同理,你也可以使用相似的代码来修改和删除防火墙规则。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值