title: Get my application to be allowed access through firewall using c#
body: <p>i am trying to get my application to be allowed through firewall, as I have to do ftp in active and passive mode is not an option as servers are not configured for that. so i tried the below code which compiles fine, I exexcute it using:</p>
  
  <pre><code> MyApp.Classes.INetFwMgr mgr = new MyApp.Classes.INetFwMgr();
      mgr.AuthorizeApplication(Application.ProductName, Application.StartupPath,
          NET_FW_SCOPE_.NET_FW_SCOPE_ALL,
          NET_FW_IP_VERSION_.NET_FW_IP_VERSION_ANY);
  </code></pre>
  
  <p>And the class which does the job:</p>
  
  <pre><code>private const string CLSID_FIREWALL_MANAGER = 
  "{304CE942-6E39-40D8-943A-B913C40C9CD4}"; 
  private static NetFwTypeLib.INetFwMgr GetFirewallManager() 
  { 
  Type objectType = Type.GetTypeFromCLSID( 
  new Guid(CLSID_FIREWALL_MANAGER)); 
  return Activator.CreateInstance(objectType) 
  as NetFwTypeLib.INetFwMgr; 
  } 
  
  
  private const string PROGID_AUTHORIZED_APPLICATION = 
  "HNetCfg.FwAuthorizedApplication"; 
  public bool AuthorizeApplication(string title, string applicationPath, 
  NET_FW_SCOPE_ scope, NET_FW_IP_VERSION_ ipVersion) 
  { 
  // Create the type from prog id 
  Type type = Type.GetTypeFromProgID(PROGID_AUTHORIZED_APPLICATION); 
  INetFwAuthorizedApplication auth = Activator.CreateInstance(type) 
  as INetFwAuthorizedApplication; 
  auth.Name = title; 
  auth.ProcessImageFileName = applicationPath; //Getting Access Denied Exception Here
  auth.Scope = scope; 
  auth.IpVersion = ipVersion; 
  auth.Enabled = true; 
  NetFwTypeLib.INetFwMgr manager = GetFirewallManager(); 
  try 
  { 
  manager.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(auth); 
  } 
  catch (Exception ex) 
  { 
  return false; 
  } 
  return true; 
  }
  </code></pre>
  
  <p>using above code, but i get <code>Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))</code> c# exception on line</p>
  
  <pre><code>auth.ProcessImageFileName = applicationPath;
  </code></pre>
  
  <p>any ideas what to do ?</p>
  
  <p><code>Edit1:</code> How would i run this as an admin using code?</p>
  
  <p><code>Edit2:</code> I also tried <code>Putting &lt;requestedExecutionLevel  level="requireAdministrator" uiAccess="false" /&gt; in manifest did not make a difference</code></p>
  
  <p>P.S.This programs execution context can be Win 7, vista, xp</p>