很多时候,我们的程序是通过网络通信(如TCP或者UDP协议+端口),而将制作好的程序安装包给客户用时,发现会出现不能通信的现象(或者在这台电脑是可以的,却在另一台不可以),原因是防火墙阻止了,需要添加防火墙例外。现在将代码记录下来,方便以后备用。
在Visual studio 项目引用右键里面添加引用,选中COM然后找到NetFwTypeLib,确认,然后新建类FireWallHelp.cs添加
using NetFwTypeLib;
代码如下
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using NetFwTypeLib;
5
6 namespace FireWallTest
7 {
8 public class FireWallHelp
9 {
10 /// <summary>
11 /// 添加防火墙例外端口
12 /// </summary>
13 /// <param name="name">名称</param>
14 /// <param name="port">端口</param>
15 /// <param name="protocol">协议(TCP、UDP)</param>
16 public static void NetFwAddPorts(string name, int port, string protocol)
17 {
18 //创建firewall管理类的实例
19 INetFwMgr netFwMgr = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr"));
20
21 INetFwOpenPort objPort = (INetFwOpenPort)Activator.CreateInstance(
22 Type.GetTypeFromProgID("HNetCfg.FwOpenPort"));
23
24 objPort.Name = name;
25 objPort.Port = port;
26 if (protocol.ToUpper() == "TCP")
27 {
28 objPort.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP;
29 }
30 else
31 {
32 objPort.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP;
33 }
34 objPort.Scope = NET_FW_SCOPE_.NET_FW_SCOPE_ALL;
35 objPort.Enabled = true;
36
37 bool exist = false;
38 //加入到防火墙的管理策略
39 foreach (INetFwOpenPort mPort in netFwMgr.LocalPolicy.CurrentProfile.GloballyOpenPorts)
40 {
41
42 if (objPort == mPort)
43 {
44 exist = true;
45 break;
46 }
47 }
48 if (exist)
49 {
50 System.Windows.Forms.MessageBox.Show("exist");
51 }
52 if (!exist) netFwMgr.LocalPolicy.CurrentProfile.GloballyOpenPorts.Add(objPort);
53 }
54 /// <summary>
55 /// 将应用程序添加到防火墙例外
56 /// </summary>
57 /// <param name="name">应用程序名称</param>
58 /// <param name="executablePath">应用程序可执行文件全路径</param>
59 public static void NetFwAddApps(string name, string executablePath)
60 {
61 //创建firewall管理类的实例
62 INetFwMgr netFwMgr = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr"));
63
64 INetFwAuthorizedApplication app = (INetFwAuthorizedApplication)Activator.CreateInstance(
65 Type.GetTypeFromProgID("HNetCfg.FwAuthorizedApplication"));
66
67 //在例外列表里,程序显示的名称
68 app.Name = name;
69
70 //程序的路径及文件名
71 app.ProcessImageFileName = executablePath;
72
73 //是否启用该规则
74 app.Enabled = true;
75
76 //加入到防火墙的管理策略
77 netFwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(app);
78
79 bool exist = false;
80 //加入到防火墙的管理策略
81 foreach (INetFwAuthorizedApplication mApp in netFwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications)
82 {
83 if (app == mApp)
84 {
85 exist = true;
86 break;
87 }
88 }
89 if (!exist) netFwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(app);
90 }
91 /// <summary>
92 /// 删除防火墙例外端口
93 /// </summary>
94 /// <param name="port">端口</param>
95 /// <param name="protocol">协议(TCP、UDP)</param>
96 public static void NetFwDelApps(int port, string protocol)
97 {
98 INetFwMgr netFwMgr = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr"));
99 if (protocol == "TCP")
100 {
101 netFwMgr.LocalPolicy.CurrentProfile.GloballyOpenPorts.Remove(port, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP);
102 }
103 else
104 {
105 netFwMgr.LocalPolicy.CurrentProfile.GloballyOpenPorts.Remove(port, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP);
106 }
107 }
108 /// <summary>
109 /// 删除防火墙例外中应用程序
110 /// </summary>
111 /// <param name="executablePath">程序的绝对路径</param>
112 public static void NetFwDelApps(string executablePath)
113 {
114 INetFwMgr netFwMgr = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr"));
115
116 netFwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Remove(executablePath);
117
118 }
119 }
120 }
转载地址:http://www.cnblogs.com/ITBread/archive/2012/04/29/2476170.html