此操作类主要使用了系统命令行来实现的,详细的命令可参照微软文档《如何使用"netsh advfirewall firewall"上下文而非“netsh firewall”上下文来控制 Windows Server 2008 和 Windows Vista 中的 Windows 防火墙行为》,而检查端口部分则利用Socket判断端口是否被占用,但是在也会有判断失灵的时候。
public static class PortHelper
{
#region 命令原型
//netsh firewall delete allowedprogram [AppPath] //netsh firewall add allowedprogram [appPath] [portName] ENABLE //netsh firewall add portopening [ALL|TCP|UDP] [portID] [portName] //netsh firewall delete portopening [ALL|TCP|UDP] [portID]
#endregion /// <summary>
/// 判定指定端口号有否被占用
/// </summary>
/// <param name="portId">端口号</param>
/// <returns></returns>
public static bool IsAvaliable(int portId)
{
bool result;
IPEndPoint point = new IPEndPoint(IPAddress.Parse("127.0.0.1"), portId);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
socket.Bind(point);
result = true;
}
catch (Exception ex)
{
result = false;
}
finally
{
socket.Close();
socket.Dispose();
socket = null;
}
return result;
} #region 指定端口号 /// <summary>
/// 开放指定端口
/// </summary>
/// <param name="portName">端口名称</param>
/// <param name="portId">端口号</param>
/// <param name="type">协议类型</param>
/// <returns></returns>
public static bool OpenPortByID(string portName, int portId,ProtocolTypeName type)
{
string arg = string.Format("firewall add portopening {0} {1} {2}", type, portId, portName);
string cmdResult = RunCMD("netsh", arg);
if (cmdResult.Contains("确定"))
return true;
return false;
} /// <summary>
/// 开放指定的所有类型端口
/// </summary>
/// <param name="portName">端口名称</param>
/// <param name="portId">端口号</param>
/// <returns></returns>
public static bool OpenPortByID(string portName, int portId)
{
return OpenPortByID(portName, portId, ProtocolTypeName.ALL);
} /// <summary>
/// 删除指定的所有类型端口
/// </summary>
/// <param name="portId">端口号</param>
/// <returns></returns>
public static bool DeletePortByID(int portId)
{
return DeletePortByID(portId, ProtocolTypeName.ALL);
} /// <summary>
/// 删除指定的端口
/// </summary>
/// <param name="portId">端口号</param>
/// <param name="type">协议类型</param>
/// <returns></returns>
public static bool DeletePortByID(int portId, ProtocolTypeName type)
{
string arg = string.Format("firewall delete portopening {0} {1}", type, portId);
string cmdResult = RunCMD("netsh", arg);
if (cmdResult.Contains("确定"))
return true;
return false;
} #endregion #region 指定应用程序 /// <summary>
/// 为指定应用程序开放端口
/// </summary>
/// <param name="appPath">应用程序路径</param>
/// <param name="portName">端口名称</param>
/// <returns></returns>
public static bool OpenPortAppName(string appPath,string portName)
{
string arg = string.Format("firewall add allowedprogram {0} {1} ENABLE",appPath,portName);
string cmdResult = RunCMD("netsh", arg);
if (cmdResult.Contains("确定"))
return true;
return false;
} /// <summary>
/// 删除指定应用程序的端口
/// </summary>
/// <param name="appPath">应用程序路径</param>
/// <returns></returns>
public static bool DeletePortByAppName(string appPath)
{
string arg = string.Format("firewall delete allowedprogram {0}",appPath);
string cmdResult = RunCMD("netsh", arg);
if (cmdResult.Contains("确定"))
return true;
return false;
} #endregion /// <summary>
/// 执行命令
/// </summary>
/// <param name="cmd">命令名称</param>
/// <param name="arg">参数</param>
/// <returns></returns>
private static string RunCMD(string cmd, string arg)
{
Process pro = new Process();
pro.StartInfo = new ProcessStartInfo(cmd, arg);
pro.StartInfo.UseShellExecute = false;
pro.StartInfo.RedirectStandardOutput = true;
pro.Start();
pro.WaitForExit();
string outputText = pro.StandardOutput.ReadToEnd();
return outputText;
}
} public enum ProtocolTypeName
{
ALL,
TCP,
UDP
}