隐藏

C# 操作系统防火墙

发布:2021/12/20 9:08:03作者:管理员 来源:本站 浏览次数:921

很多时候,我们的程序是通过网络通信(如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. namespace FireWallTest
  6. {
  7. public class FireWallHelp
  8. {
  9. /// <summary>
  10. /// 添加防火墙例外端口
  11. /// </summary>
  12. /// <param name="name">名称</param>
  13. /// <param name="port">端口</param>
  14. /// <param name="protocol">协议(TCP、UDP)</param>
  15. public static void NetFwAddPorts(string name, int port, string protocol)
  16. {
  17. //创建firewall管理类的实例
  18. INetFwMgr netFwMgr = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr"));
  19. INetFwOpenPort objPort = (INetFwOpenPort)Activator.CreateInstance(
  20. Type.GetTypeFromProgID("HNetCfg.FwOpenPort"));
  21. objPort.Name = name;
  22. objPort.Port = port;
  23. if (protocol.ToUpper() == "TCP")
  24. {
  25. objPort.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP;
  26. }
  27. else
  28. {
  29. objPort.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP;
  30. }
  31. objPort.Scope = NET_FW_SCOPE_.NET_FW_SCOPE_ALL;
  32. objPort.Enabled = true;
  33. bool exist = false;
  34. //加入到防火墙的管理策略
  35. foreach (INetFwOpenPort mPort in netFwMgr.LocalPolicy.CurrentProfile.GloballyOpenPorts)
  36. {
  37. if (objPort == mPort)
  38. {
  39. exist = true;
  40. break;
  41. }
  42. }
  43. if (exist)
  44. {
  45. System.Windows.Forms.MessageBox.Show("exist");
  46. }
  47. if (!exist) netFwMgr.LocalPolicy.CurrentProfile.GloballyOpenPorts.Add(objPort);
  48. }
  49. /// <summary>
  50. /// 将应用程序添加到防火墙例外
  51. /// </summary>
  52. /// <param name="name">应用程序名称</param>
  53. /// <param name="executablePath">应用程序可执行文件全路径</param>
  54. public static void NetFwAddApps(string name, string executablePath)
  55. {
  56. //创建firewall管理类的实例
  57. INetFwMgr netFwMgr = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr"));
  58. INetFwAuthorizedApplication app = (INetFwAuthorizedApplication)Activator.CreateInstance(
  59. Type.GetTypeFromProgID("HNetCfg.FwAuthorizedApplication"));
  60. //在例外列表里,程序显示的名称
  61. app.Name = name;
  62. //程序的路径及文件名
  63. app.ProcessImageFileName = executablePath;
  64. //是否启用该规则
  65. app.Enabled = true;
  66. //加入到防火墙的管理策略
  67. netFwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(app);
  68. bool exist = false;
  69. //加入到防火墙的管理策略
  70. foreach (INetFwAuthorizedApplication mApp in netFwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications)
  71. {
  72. if (app == mApp)
  73. {
  74. exist = true;
  75. break;
  76. }
  77. }
  78. if (!exist) netFwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(app);
  79. }
  80. /// <summary>
  81. /// 删除防火墙例外端口
  82. /// </summary>
  83. /// <param name="port">端口</param>
  84. /// <param name="protocol">协议(TCP、UDP)</param>
  85. public static void NetFwDelApps(int port, string protocol)
  86. {
  87. INetFwMgr netFwMgr = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr"));
  88. if (protocol == "TCP")
  89. {
  90. netFwMgr.LocalPolicy.CurrentProfile.GloballyOpenPorts.Remove(port, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP);
  91. }
  92. else
  93. {
  94. netFwMgr.LocalPolicy.CurrentProfile.GloballyOpenPorts.Remove(port, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP);
  95. }
  96. }
  97. /// <summary>
  98. /// 删除防火墙例外中应用程序
  99. /// </summary>
  100. /// <param name="executablePath">程序的绝对路径</param>
  101. public static void NetFwDelApps(string executablePath)
  102. {
  103. INetFwMgr netFwMgr = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr"));
  104. netFwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Remove(executablePath);
  105. }
  106. }
  107. }