Manage Windows Firewall programmatically

Source: Internet
Author: User
Tags assert tostring firewall

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
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.