一个端口操作类PortHelper
此操作类主要使用了系统命令行来实现的,详细的命令可参照微软文档《如何使用"netsh advfirewall firewall"上下文而非“netsh firewall”上下文来控制 Windows Server 2008 和 Windows Vista 中的 Windows 防火墙行为》,而检查端口部分则利用Socket判断端口是否被占用,但是在也会有判断失灵的时候。
1 public static class PortHelper 2 { 3 #region 命令原型 4 //netsh firewall delete allowedprogram [AppPath] 5 6 //netsh firewall add allowedprogram [appPath] [portName] ENABLE 7 8 //netsh firewall add portopening [ALL|TCP|UDP] [portID] [portName] 9 10 //netsh firewall delete portopening [ALL|TCP|UDP] [portID] 11 #endregion 12 13 /// <summary> 14 /// 判定指定端口号有否被占用 15 /// </summary> 16 /// <param name="portId">端口号</param> 17 /// <returns></returns> 18 public static bool IsAvaliable(int portId) 19 { 20 bool result; 21 IPEndPoint point = new IPEndPoint(IPAddress.Parse("127.0.0.1"), portId); 22 Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 23 try 24 { 25 socket.Bind(point); 26 result = true; 27 } 28 catch (Exception ex) 29 { 30 result = false; 31 } 32 finally 33 { 34 socket.Close(); 35 socket.Dispose(); 36 socket = null; 37 } 38 return result; 39 } 40 41 #region 指定端口号 42 43 /// <summary> 44 /// 开放指定端口 45 /// </summary> 46 /// <param name="portName">端口名称</param> 47 /// <param name="portId">端口号</param> 48 /// <param name="type">协议类型</param> 49 /// <returns></returns> 50 public static bool OpenPortByID(string portName, int portId,ProtocolTypeName type) 51 { 52 string arg = string.Format("firewall add portopening {0} {1} {2}", type, portId, portName); 53 string cmdResult = RunCMD("netsh", arg); 54 if (cmdResult.Contains("确定")) 55 return true; 56 return false; 57 } 58 59 /// <summary> 60 /// 开放指定的所有类型端口 61 /// </summary> 62 /// <param name="portName">端口名称</param> 63 /// <param name="portId">端口号</param> 64 /// <returns></returns> 65 public static bool OpenPortByID(string portName, int portId) 66 { 67 return OpenPortByID(portName, portId, ProtocolTypeName.ALL); 68 } 69 70 /// <summary> 71 /// 删除指定的所有类型端口 72 /// </summary> 73 /// <param name="portId">端口号</param> 74 /// <returns></returns> 75 public static bool DeletePortByID(int portId) 76 { 77 return DeletePortByID(portId, ProtocolTypeName.ALL); 78 } 79 80 /// <summary> 81 /// 删除指定的端口 82 /// </summary> 83 /// <param name="portId">端口号</param> 84 /// <param name="type">协议类型</param> 85 /// <returns></returns> 86 public static bool DeletePortByID(int portId, ProtocolTypeName type) 87 { 88 string arg = string.Format("firewall delete portopening {0} {1}", type, portId); 89 string cmdResult = RunCMD("netsh", arg); 90 if (cmdResult.Contains("确定")) 91 return true; 92 return false; 93 } 94 95 #endregion 96 97 #region 指定应用程序 98 99 /// <summary> 100 /// 为指定应用程序开放端口 101 /// </summary> 102 /// <param name="appPath">应用程序路径</param> 103 /// <param name="portName">端口名称</param> 104 /// <returns></returns> 105 public static bool OpenPortAppName(string appPath,string portName) 106 { 107 string arg = string.Format("firewall add allowedprogram {0} {1} ENABLE",appPath,portName); 108 string cmdResult = RunCMD("netsh", arg); 109 if (cmdResult.Contains("确定")) 110 return true; 111 return false; 112 } 113 114 /// <summary> 115 /// 删除指定应用程序的端口 116 /// </summary> 117 /// <param name="appPath">应用程序路径</param> 118 /// <returns></returns> 119 public static bool DeletePortByAppName(string appPath) 120 { 121 string arg = string.Format("firewall delete allowedprogram {0}",appPath); 122 string cmdResult = RunCMD("netsh", arg); 123 if (cmdResult.Contains("确定")) 124 return true; 125 return false; 126 } 127 128 #endregion 129 130 /// <summary> 131 /// 执行命令 132 /// </summary> 133 /// <param name="cmd">命令名称</param> 134 /// <param name="arg">参数</param> 135 /// <returns></returns> 136 private static string RunCMD(string cmd, string arg) 137 { 138 Process pro = new Process(); 139 pro.StartInfo = new ProcessStartInfo(cmd, arg); 140 pro.StartInfo.UseShellExecute = false; 141 pro.StartInfo.RedirectStandardOutput = true; 142 pro.Start(); 143 pro.WaitForExit(); 144 string outputText = pro.StandardOutput.ReadToEnd(); 145 return outputText; 146 } 147 } 148 149 public enum ProtocolTypeName 150 { 151 ALL, 152 TCP, 153 UDP 154 }