// example: RemoveFromPermissionsList(Application.ExecutablePath); public static void RemoveFromPermissionsList(string imageName) { // Remove the application from the ICF Permissions List NetFwTypeLib.INetFwMgr mgr = (NetFwTypeLib.INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr")); mgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Remove(imageName); }
/// <summary> /// Determines whether the windows firewall is has configuration elements for the specified application name. /// </summary> /// <param name="appName">Name of the application to test for firewall configuration</param> /// <returns>boolean indicating whether firewall is configured for the specified application</returns> public bool IsFirewallApplicationConfigured(string appName) { bool bFound = false; Type typeFWMgr = Type.GetTypeFromProgID("HNetCfg.FwMgr"); NetFwTypeLib.INetFwMgr manager = Activator.CreateInstance(typeFWMgr) as NetFwTypeLib.INetFwMgr; if (manager == null) { return(false); } // check applications list System.Collections.IEnumerator appEnumerate = manager.LocalPolicy.CurrentProfile.AuthorizedApplications.GetEnumerator(); if (appEnumerate == null) { return(false); } while (appEnumerate.MoveNext() && bFound == false) { NetFwTypeLib.INetFwAuthorizedApplication app = appEnumerate.Current as NetFwTypeLib.INetFwAuthorizedApplication; if (app != null && app.Name == appName && app.Enabled == true) { bFound = true; } } return(bFound); }
/// <summary> /// Adds the specified application name, located at the specific path, to the firewall rules /// </summary> /// <param name="appName">name of the application to add</param> /// <param name="ExecutablePath">file path of the application to add</param> public void AddFirewallApplicationEntry(string appName, string ExecutablePath) { try { Type typeFWMgr = Type.GetTypeFromProgID("HNetCfg.FwMgr"); NetFwTypeLib.INetFwMgr manager = Activator.CreateInstance(typeFWMgr) as NetFwTypeLib.INetFwMgr; if (manager == null) { throw new ApplicationException("Unable to connect to fireall."); } Type typeApp = Type.GetTypeFromProgID("HNetCfg.FwAuthorizedApplication"); NetFwTypeLib.INetFwAuthorizedApplication app = Activator.CreateInstance(typeApp) as NetFwTypeLib.INetFwAuthorizedApplication; if (app == null) { throw new ApplicationException("Unable to create new Firewall Application reference."); } app.Enabled = true; //app.IpVersion = NET_FW_IP_VERSION_.NET_FW_IP_VERSION_V4; app.Name = appName; app.ProcessImageFileName = ExecutablePath;// System.Windows.Forms.Application.ExecutablePath; app.Scope = NET_FW_SCOPE_.NET_FW_SCOPE_ALL; manager.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(app); } catch (Exception ex) { Trace.WriteLine("FirewallWrapper: Exception " + ex.ToString(), "FIREWALL"); } }
// example: AddToPermissionsList("MyTest", Application.ExecutablePath, true); public static void AddToPermissionsList(string name, string imageName, bool enabled) { // Add the application to the ICF Permissions List NetFwTypeLib.INetFwMgr mgr = (NetFwTypeLib.INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr")); NetFwTypeLib.INetFwAuthorizedApplication app = (NetFwTypeLib.INetFwAuthorizedApplication) Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwAuthorizedApplication")); app.Name = name; app.ProcessImageFileName = imageName; app.Enabled = enabled; mgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(app); }
/// <summary> /// Determines whether the windows firewall is enabled. /// </summary> /// <returns>boolean indicating whether firewall is enabled</returns> public bool IsFirewallDisabled() { Type typeFWMgr = Type.GetTypeFromProgID("HNetCfg.FwMgr"); NetFwTypeLib.INetFwMgr manager = Activator.CreateInstance(typeFWMgr) as NetFwTypeLib.INetFwMgr; if (manager == null) { return(false); } // check if Firewall is enabled if (manager.LocalPolicy.CurrentProfile.FirewallEnabled == false) { return(true); } return(false); }