Recent projects need to automatically set up Windows Firewall through the program, check the data, you can use the command line netsh firewall to achieve. Encapsulates a class to manage the program that allows for release (allowed). Managing other content, such as unlocking ports, is similar.
The program uses a common class runprocess, which can be obtained from another article written by a C # calling external Process class
Namespace Winfirewall
{
public enum Tscope
{
All,
SUBNET,
CUSTOM,
}
public enum Tmode
{
ENABLE,
DISABLE,
}
/**////<summary>
Manage the allowed program with the Windows Firewall.
</summary>
public class Allowedprogram
{
Set Allowedprogram help#region set Allowedprogram Help
/**//*
Set Allowedprogram
[Program =] Path
[[name =] Name
[mode =] enable| DISABLE
[scope =] all| Subnet| CUSTOM
[addresses =] addresses
[profile =] Current| domain| standard| ALL]
Sets firewall allowed program configuration.
Parameters:
Program-program path and file name.
Name-program name (optional).
Mode-program mode (optional).
Enable-allow through firewall (default).
Disable-do not allow through firewall.
Scope-program scope (optional).
All-allow all traffic through firewall (default).
Subnet-allow only the local network (SUBNET) traffic through firewall.
Custom-allow only specified traffic through firewall.
Addresses-custom scope addresses (optional).
Profile-configuration profile (optional).
Current-current profile (default).
Domain-domain profile.
Standard-standard profile.
All-all profiles.
Remarks: ' Scope ' must be ' CUSTOM ' to specify ' addresses '.
Examples:
Set Allowedprogram C:myappmyapp.exe MyApp ENABLE
Set Allowedprogram C:myappmyapp.exe MyApp DISABLE
Set Allowedprogram c:myappmyapp.exe MyApp ENABLE CUSTOM
157.60.0.1,172.16.0.0/16,10.0.0.0/255.0.0.0,localsubnet
Set Allowedprogram program = c:myappmyapp.exe name = MyApp mode = ENABLE
Set Allowedprogram program = c:myappmyapp.exe name = MyApp mode = DISABLE
Set Allowedprogram program = c:myappmyapp.exe name = MyApp mode = ENABLE
Scope = CUSTOM addresses =
157.60.0.1,172.16.0.0/16,10.0.0.0/255.0.0.0,localsubnet
*/
#endregion
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
Public Property#region Public
/**////<summary>
Program path and file name.
</summary>
Public String Program
{
Get
{
return m_program;
}
Set
{
M_program = value;
}
}
/**////<summary>
Program name (optional).
</summary>
Public String Name
{
Get
{
return m_name;
}
Set
{
M_name = value;
}
}
/**////<summary>
Program scope (optional).
All-allow all traffic through firewall (default).
Subnet-allow only the local network (SUBNET) traffic through firewall.
Custom-allow only specified traffic through firewall. </summary>
Public Tscope Scope
{
Get
{
return m_scope;
}
Set
{
M_scope = value;
}
}
/**////<summary>
Program mode (optional).
Enable-allow through firewall (default).
Disable-do not allow through firewall
</summary>
Public Tmode Mode
{
Get
{
return m_mode;
}
Set
{
M_mode = value;
}
}
/**////<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
{
Get
{
return m_address;
}
Set
{
m_address = value;
}
}
#endregion
Public Method#region Public method
/**////<summary>
Set allowed Program
</summary>
public void Set ()
{
Debug.Assert (program!= NULL);
if (Name = = null)
{
Name = System.IO.Path.GetFileNameWithoutExtension (program);
}
if (Scope = = Tscope.custom)
{
Debug.Assert (address!= null);
}
Runprocess runcmd = new runprocess ();
String command;
Command = String.Format ("firewall set allowedprogram {0} {1} {2} {3}",
Program, Name, Mode.tostring (), scope.tostring ());
if (Scope = = Tscope.custom)
{
Command + = "" + Address;
}
Runcmd.run ("netsh", command);
if (runcmd.error!= null && runcmd.error!= "")
{
throw new Exception (Runcmd.error);
}
if (!runcmd.output.tolower (). Contains ("OK.")
{
throw new Exception (runcmd.output);
}
}
/**////<summary>
Delete allowed Program
</summary>
public void Delete ()
{
Debug.Assert (program!= NULL);
Runprocess runcmd = new runprocess ();
String command = String.Format ("firewall delete allowedprogram {0}",
program);
Runcmd.run ("netsh", command);
if (runcmd.error!= null && runcmd.error!= "")
{
throw new Exception (Runcmd.error);
}
if (!runcmd.output.tolower (). Contains ("OK.")
{
throw new Exception (runcmd.output);
}
}
#endregion
}
}