C#HttpListener没有使用netsh来注册URI [英] C# HttpListener without using netsh to register a URI
问题描述
我的应用程序使用一个小型Web服务器来为某些文件提供服务,并具有一个用于远程管理的Web界面。现在,用户必须使用netsh注册URI,因此
My application uses a small webserver to server up some files and have a web interface for administration remotely. Right now the user has to use netsh to register the URI like so
netsh http add urlacl url=http://+:1233/ user=Chris-PC\Chris
对于普通用户来说这并不好玩。我希望程序能够在我的程序中监听用户指定的任何端口,而不需要最终用户使用命令提示符。有没有办法完成这个简单的使用Process.Start并自己运行命令提示符?
Which is no fun for the average user. I'd like the program to be able to listen on any port specified by the user from my program without the end-user needing to using command prompt. Is there anyway to accomplish this short of just using Process.Start and running command prompt myself?
推荐答案
我写这个来提升烫发并通过 netsh 添加http ACL条目。
I wrote this to elevate perms and add http ACL entries through netsh.
系统会提示用户在系统中进行更改,但总比没有好。您可能希望这样做以响应 AddressAccessDeniedException
Users will get prompted to make changes top their system but it's better than nothing. You might want to do this in response to an AddressAccessDeniedException
public static class NetAclChecker
{
public static void AddAddress(string address)
{
AddAddress(address, Environment.UserDomainName, Environment.UserName);
}
public static void AddAddress(string address, string domain, string user)
{
string args = string.Format(@"http add urlacl url={0} user={1}\{2}", address, domain, user);
ProcessStartInfo psi = new ProcessStartInfo("netsh", args);
psi.Verb = "runas";
psi.CreateNoWindow = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.UseShellExecute = true;
Process.Start(psi).WaitForExit();
}
}
这篇关于C#HttpListener没有使用netsh来注册URI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!