Search code examples
c#wcfadminnetsh

How to start a C# console application as administrator


I have a wcf web service that I am running via console. If I start the service as an admin i.e. right click the executable an run as administrator then it works, but if I just open it it complains and says "http could not register url http //+ 80/ . . .". Alternatively I could run netsh within my c# code and add the URL (I suppose), but that too needs admin privileges. I tried: this code , GetIP() obtains the string literal of the ip address v4

ProcessStartInfo procInfo = new ProcessStartInfo
{
    WorkingDirectory = Path.GetPathRoot(Environment.SystemDirectory),
    FileName = Path.Combine(Environment.SystemDirectory, "netsh.exe"),
    Arguments = "netsh http add urlacl url=http://" + GetIP() + ":80/VSPDataLogger/ user=everyone",
    RedirectStandardOutput = true,
    RedirectStandardError = true,
    UseShellExecute = false,
    CreateNoWindow = true,
    WindowStyle = ProcessWindowStyle.Hidden
};

Process proc = Process.Start(procInfo);
proc.WaitForExit();

It would be nice if I can start the application without prompting the user to start as admin (UAC nonsense). I tried adding the manifest file and changing the execution level to admin but that didn't seem to work or my application is ignoring the file (I don't know).


Solution

  • You will not get this working without UAC prompt. It's working as designed.