Search code examples
c#windowsprocessadminprocessstartinfo

C# Start Program with ADMIN rights from other program


I´ve written a notify program, which communicats with an windows service on WebSocket. This notify program does not have admin rights but with click on button starts an other program which starts and stops the service. This program should run with admin rights, it worked very fine for some time but somehow not any more.

Thats my code snippet:

        using (var process = new Process())
        {
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.FileName = $".\\ServiceControl.exe";
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.Arguments = $"-s {ServiceName} -start";
            process.StartInfo.Verb = "runas";
            process.Start();
        }

The exception is the following:

System.ComponentModel.Win32Exception: "Der angeforderte Vorgang erfordert erhöhte Rechte"

Back then it showed a window where I could accept admin rights but does not any more.


Solution

  • Try adding this line

    process.StartInfo.UseShellExecute = true;
    

    on

    using (var process = new Process())
    {
        process.StartInfo.UseShellExecute = true;
        process.StartInfo.FileName = $".\\ServiceControl.exe";
        process.StartInfo.CreateNoWindow = true;
        process.StartInfo.Arguments = $"-s {ServiceName} -start";
        process.StartInfo.Verb = "runas";
        process.Start();
    }