【发布时间】:2012-01-26 04:52:09
【问题描述】:
我试图让我的应用程序被允许通过防火墙,因为我必须在主动和被动模式下执行 ftp 不是一个选项,因为服务器没有为此配置。所以我尝试了以下编译好的代码,我使用它执行它:
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);
以及完成这项工作的班级:
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;
}
使用上面的代码,但我得到Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) c# 异常在线
auth.ProcessImageFileName = applicationPath;
有什么想法吗?
Edit1:我如何以管理员身份使用代码运行它?
Edit2:我也试过Putting <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> in manifest did not make a difference
P.S.本程序执行上下文可以是Win 7, vista, xp
【问题讨论】:
-
仅供参考,没有“C#.net”之类的东西。
-
这段代码的执行上下文是什么?常规程序当然不仅可以强制打开防火墙,这会破坏拥有防火墙的意义。至少需要管理员权限,您通常只能在安装程序中获得这种权限。在服务器场景中,您肯定希望由 LAN 管理员明确授予访问权限。