Created
July 23, 2012 05:42
-
-
Save sandrinodimattia/3162120 to your computer and use it in GitHub Desktop.
NetFwMgr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class NetFwMgr : DynamicComBase | |
{ | |
/// <summary> | |
/// Default constructor. | |
/// </summary> | |
public NetFwMgr() | |
{ | |
// Initialize the COM object. | |
Type fwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr"); | |
base.comObj = Activator.CreateInstance(fwMgrType); | |
} | |
/// <summary> | |
/// Set the port in the firewall. | |
/// </summary> | |
/// <param name="profile"></param> | |
/// <param name="name"></param> | |
/// <param name="protocol"></param> | |
/// <param name="port"></param> | |
/// <param name="scope"></param> | |
/// <param name="enabled"></param> | |
public void SetPort(NetFwProfile profile, string name, NetFwIpProtocol protocol, int port, NetFwScope scope, bool enabled) | |
{ | |
if (profile == NetFwProfile.All) | |
throw new ArgumentException("The profile 'All' is not allowed here."); | |
// Create a port object. | |
Type fwPortType = Type.GetTypeFromProgID("HNetCfg.FwOpenPort"); | |
dynamic fwPort = Activator.CreateInstance(fwPortType); | |
// Configure the port. | |
fwPort.Name = name; | |
fwPort.Protocol = (int)protocol; | |
fwPort.Port = port; | |
fwPort.Scope = (int)scope; | |
fwPort.Enabled = enabled; | |
// Add to profile. | |
comObj.LocalPolicy.GetProfileByType((int)profile).GloballyOpenPorts.Add(fwPort); | |
} | |
/// <summary> | |
/// Verify if the firewall is enabled. | |
/// Note, this will fail if the service is stopped. | |
/// </summary> | |
public bool IsEnabled | |
{ | |
get | |
{ | |
return comObj.LocalPolicy.CurrentProfile.FirewallEnabled; | |
} | |
} | |
/// <summary> | |
/// Get a list of authorized applications. | |
/// </summary> | |
public List<NetFwAuthorizedApplication> GetAuthorizedApplications() | |
{ | |
// Create a new list. | |
List<NetFwAuthorizedApplication> applications = new List<NetFwAuthorizedApplication>(); | |
// Get all applications and create typed objects. | |
foreach (dynamic application in comObj.LocalPolicy.CurrentProfile.AuthorizedApplications) | |
{ | |
applications.Add(new NetFwAuthorizedApplication(application)); | |
// Clean current COM object. | |
ReleaseCom(application); | |
} | |
// Clean up all released COM objects. | |
CleanAllReleased(); | |
// Done. | |
return applications; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment