最近项目中需要通过程序自动设置windows 防火墙,查了一下资料,可以通过命令行netsh firewall来实现。封装了一个类来实现对允许放开的程序(Allowed program)进行管理。管理其他内容比如放开端口等方法是类似的。
通过编程管理windows 防火墙
作者:肖波
最近项目中需要通过程序自动设置windows 防火墙,查了一下资料,可以通过命令行netsh firewall来实现。封装了一个类来实现对允许放开的程序(Allowed program)进行管理。管理其他内容比如放开端口等方法是类似的。
程序中用到一个公共类 RunProcess,这个类可从我的另一篇文章
一个C#写的调用外部进程类 获得
namespace WinFirewall
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public enum TScope
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ALL,
SUBNET,
CUSTOM,
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public enum TMode
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ENABLE,
DISABLE,
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Manage the allowed program with the Windows Firewall.
/// </summary>
public class AllowedProgram
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Set AllowedProgram Help#region Set AllowedProgram Help
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*
set allowedprogram
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[ program = ] path
[ [ name = ] name
[ mode = ] ENABLE|DISABLE
[ scope = ] ALL|SUBNET|CUSTOM
[ addresses = ] addresses
[ profile = ] CURRENT|DOMAIN|STANDARD|ALL ]
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Sets firewall allowed program configuration.
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Parameters:
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
program - Program path and file name.
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
name - Program name (optional).
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
mode - Program mode (optional).
ENABLE - Allow through firewall (default).
DISABLE - Do not allow through firewall.
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
scope - Program scope (optional).
ALL - Allow all traffic through firewall (default).
SUBNET - Allow only local network (subnet) traffic through firewall.
CUSTOM - Allow only specified traffic through firewall.
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
addresses - Custom scope addresses (optional).
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
profile - Configuration profile (optional).
CURRENT - Current profile (default).
DOMAIN - Domain profile.
STANDARD - Standard profile.
ALL - All profiles.
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Remarks: 'scope' must be 'CUSTOM' to specify 'addresses'.
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Examples:
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
set allowedprogram C:\MyApp\MyApp.exe MyApp ENABLE
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
set allowedprogram C:\MyApp\MyApp.exe MyApp DISABLE
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
set allowedprogram C:\MyApp\MyApp.exe MyApp ENABLE CUSTOM
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
157.60.0.1,172.16.0.0/16,10.0.0.0/255.0.0.0,LocalSubnet
set allowedprogram program = C:\MyApp\MyApp.exe name = MyApp mode = ENABLE
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
set allowedprogram program = C:\MyApp\MyApp.exe name = MyApp mode = DISABLE
set allowedprogram program = C:\MyApp\MyApp.exe name = MyApp mode = ENABLE
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
scope = CUSTOM addresses =
157.60.0.1,172.16.0.0/16,10.0.0.0/255.0.0.0,LocalSubnet
*/
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
private field#region private field
private String m_Program;
private String m_Name;
private TScope m_Scope = TScope.ALL;
private TMode m_Mode = TMode.ENABLE;
private String m_Address;
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
public property#region public property
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Program path and file name.
/// </summary>
public String Program
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return m_Program;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
m_Program = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Program name (optional).
/// </summary>
public String Name
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return m_Name;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
m_Name = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Program scope (optional).
/// ALL - Allow all traffic through firewall (default).
/// SUBNET - Allow only local network (subnet) traffic through firewall.
/// CUSTOM - Allow only specified traffic through firewall. /// </summary>
public TScope Scope
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return m_Scope;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
m_Scope = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Program mode (optional).
/// ENABLE - Allow through firewall (default).
/// DISABLE - Do not allow through firewall
/// </summary>
public TMode Mode
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return m_Mode;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
m_Mode = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Custom scope addresses (optional).
/// </summary>
/// <example>
/// 157.60.0.1,172.16.0.0/16,10.0.0.0/255.0.0.0
/// </example>
public String Address
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return m_Address;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
m_Address = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
public method#region public method
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Set allowed program
/// </summary>
public void Set()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Debug.Assert(Program != null);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (Name == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Name = System.IO.Path.GetFileNameWithoutExtension(Program);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (Scope == TScope.CUSTOM)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Debug.Assert(Address != null);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
RunProcess runCmd = new RunProcess();
String command;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
command = String.Format("firewall set allowedprogram {0} {1} {2} {3}",
Program, Name, Mode.ToString(), Scope.ToString());
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (Scope == TScope.CUSTOM)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
command += " " + Address;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
runCmd.Run("netsh", command);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (runCmd.Error != null && runCmd.Error != "")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new Exception(runCmd.Error);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (!runCmd.Output.ToLower().Contains("ok."))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new Exception(runCmd.Output);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Delete allowed program
/// </summary>
public void Delete()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Debug.Assert(Program != null);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
RunProcess runCmd = new RunProcess();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
String command = String.Format("firewall delete allowedprogram {0}",
Program);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
runCmd.Run("netsh", command);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (runCmd.Error != null && runCmd.Error != "")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new Exception(runCmd.Error);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (!runCmd.Output.ToLower().Contains("ok."))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new Exception(runCmd.Output);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
调用的相关例程
private void buttonSetAllowProgram_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
AllowedProgram allowedProgram = new AllowedProgram();
allowedProgram.Program = textBoxProgramFilePath.Text.Trim();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (checkBoxEnable.Checked)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
allowedProgram.Mode = TMode.ENABLE;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
allowedProgram.Mode = TMode.DISABLE;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
allowedProgram.Scope = (TScope)comboBoxScope.SelectedItem;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
allowedProgram.Address = textBoxAddress.Text.Trim();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
allowedProgram.Set();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
MessageBox.Show("OK", "Information", MessageBoxButtons.OK);
}
catch (Exception e1)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox.Show(e1.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
private void buttonDelAllowProgram_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
AllowedProgram allowedProgram = new AllowedProgram();
allowedProgram.Program = textBoxProgramFilePath.Text.Trim();
allowedProgram.Delete();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
MessageBox.Show("OK", "Information", MessageBoxButtons.OK);
}
catch (Exception e1)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox.Show(e1.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}