lunes, 6 de octubre de 2014

Agregar exepciones al firewall desde C#

Primero agregamos la referencia a la dll:

Microsoft.TeamFoundation.Common


Luego pegamos el código:


public static INetFwMgr WinFirewallManager()
{
Type type = Type.GetTypeFromCLSID(
new Guid("{304CE942-6E39-40D8-943A-B913C40C9CD4}"));
return Activator.CreateInstance(type) as INetFwMgr;
}

public static bool AuthorizeProgram(string title, string path,
NET_FW_SCOPE_ scope, NET_FW_IP_VERSION_ ipver)
{
Type type = Type.GetTypeFromProgID("HNetCfg.FwAuthorizedApplication");
INetFwAuthorizedApplication authapp = Activator.CreateInstance(type)
as INetFwAuthorizedApplication;
authapp.Name = title;
authapp.ProcessImageFileName = path;
authapp.Scope = scope;
authapp.IpVersion = ipver;
authapp.Enabled = true;

INetFwMgr mgr = WinFirewallManager();
try
{
mgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(authapp);
}
catch (Exception ex)
{
System.Diagnostics.Trace.Write(ex.Message);
return false;
}
return true;
}


Y por ultimo lo llamamos de la siguiente forma:


AuthorizeProgram("TCP", @"C:\TCP.exe", NET_FW_SCOPE_.NET_FW_SCOPE_ALL, NET_FW_IP_VERSION_.NET_FW_IP_VERSION_ANY);

No hay comentarios:

Publicar un comentario