The project requires HttpListener to provide a simple httpserver service, but the operation is prompt:
UnhandledException Message: Access Denied
In System.Net.HttpListener.AddAll ()
In System.Net.HttpListener.Start ()
Online check, as if the problem of UAC permissions, the Internet found the following solution:
The manifest profile that the program has added administrator privileges to run is still not resolved, and the manual Setup program runs with administrator privileges and can be
Ways to solve the problem:
Run cmd with administrator privileges
Input: netsh http add urlacl url=http://+:9527/icon user=domain\user
Note: DOMAIN is the computer name
Like my user=cmdszh_comperny\cmdszh.
Write the above action as C # code as follows (the main program must configure UAC permissions in manifest)
public static void AddAddress ( String address)
{
Try
{
AddAddress (address, environment.userdomainname, environment.username);
} catch (Exception ex) {}
}
public static void AddAddress (string address, string domain, string user)
{
String argsdll = String.Format (@ "http delete urlacl url={0}", address);
string args = string. Format (@ "http add urlacl url={0} user={1}\{2}", address, domain, user);
ProcessStartInfo psi = new ProcessStartInfo ("netsh", argsdll);
Psi. Verb = "RunAs";
Psi. CreateNoWindow = true;
Psi. WindowStyle = Processwindowstyle.hidden;
Psi. UseShellExecute = false;
Process.Start (PSI). WaitForExit ();//Delete Urlacl
PSI = new ProcessStartInfo ("netsh", args);
Psi. Verb = "RunAs";
Psi. CreateNoWindow = true;
Psi. WindowStyle = Processwindowstyle.hidden;
Psi. UseShellExecute = false;
Process.Start (PSI). WaitForExit ();//Add Urlacl
}
Original address: http://www.cnblogs.com/cmdszh/archive/2012/08/16/httplistener.html