Sto scrivendo un server HTTP in C#.Accesso HttpListener negato
Quando provo ad eseguire la funzione HttpListener.Start()
ottengo una HttpListenerException
dicendo
"Accesso negato".
Quando eseguo l'app in modalità amministratore in Windows 7 funziona correttamente.
Posso farlo funzionare senza la modalità amministratore? se sì come? In caso contrario, come posso fare in modo che l'app passi alla modalità amministratore dopo l'avvio?
using System;
using System.Net;
namespace ConsoleApplication1
{
class Program
{
private HttpListener httpListener = null;
static void Main(string[] args)
{
Program p = new Program();
p.Server();
}
public void Server()
{
this.httpListener = new HttpListener();
if (httpListener.IsListening)
throw new InvalidOperationException("Server is currently running.");
httpListener.Prefixes.Clear();
httpListener.Prefixes.Add("http://*:4444/");
try
{
httpListener.Start(); //Throws Exception
}
catch (HttpListenerException ex)
{
if (ex.Message.Contains("Access is denied"))
{
return;
}
else
{
throw;
}
}
}
}
}
Se qualcuno vuole evitare che l'errore si può provare a scrivere con TcpListener . Non richiede i privilegi di amministratore – Vlad