获得我的应用程序使用C#允许通过防火墙访问 [英] Get my application to be allowed access through firewall using c#
问题描述
我试图让我的应用程序通过防火墙允许的,因为我必须做的ftp在主动和被动模式是不是因为服务器未配置为一个选项。所以我尝试下面的code的编译好,我exexcute它使用:
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:
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);
和类由它来完成的工作:
And the class which does the job:
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,但我得到访问被拒绝。 (从HRESULT异常:0X80070005(E_ACCESSDENIED))
C对线#异常
auth.ProcessImageFileName = applicationPath;
任何想法怎么办?
any ideas what to do ?
EDIT1:
使用code我将如何运行这个作为管理员
How would i run this as an admin using code?
EDIT2:
我也试过把< requestedExecutionLevel水平=requireAdministratoruiAccess =FALSE/>清单中并没有发挥作用
PSThis程序执行环境可以运7,VISTA,XP
P.S.This programs execution context can be Win 7, vista, xp
推荐答案
我观察到,如果我改变FTP下载语句的顺序来下列窗口对话框提示该你要允许此程序通过防火墙访问
;如果我点击允许访问的code完美的作品。
i have observed that if i change the order of ftp download statements to following windows dialog appears asking that do you want to allow this program access through firewall
; if i click allow access the code works perfectly.
requestDownload = (FtpWebRequest)WebRequest.Create(uri);
requestDownload.UsePassive = false;
requestDownload.KeepAlive = false;
requestDownload.UseBinary = true;
requestDownload.Method = WebRequestMethods.Ftp.DownloadFile;
requestDownload.Credentials = new NetworkCredential(ftpInfoDownload[3], ftpInfoDownload[4]);
responseDownload = (FtpWebResponse)requestDownload.GetResponse();
Stream ftpStream = responseDownload.GetResponseStream();
这篇关于获得我的应用程序使用C#允许通过防火墙访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!