How to run a process asynchronously in administration mode?

  admin-rights, async-await, c++, process, windows

I need my app to require admin rights before running a process asynchronously. It used to work well with the following configuration in the app.manifest:

 <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

However, since now processes were added that should not require admin rights to be run, this elegant solution no longer cuts it. This is what I have:

public async Task ExecuteElevatedProcessAsync(string executablePathArg)
{
    using (var process = new Process())
    {
        process.StartInfo.FileName = executablePathArg;
        process.StartInfo.UseShellExecute = true;
        process.StartInfo.CreateNoWindow = true;
        process.StartInfo.Verb = "runas";
        await RunAsync(process);
    };
}

private Task RunAsync(Process processArg)
{
    var taskCompletionSrc = new TaskCompletionSource<object>();
    processArg.EnableRaisingEvents = true;
    processArg.Exited += (s, e) => taskCompletionSrc.TrySetResult(null);
    if (!processArg.Start())
    {
        taskCompletionSrc.SetException(new Exception("Some descriptive error-message."));
    }
    return taskCompletionSrc.Task;
}

There must be an error in the code, since it runs the process as expected, but doesn’t actually require admin rights to do so. I expected this to do the trick:

process.StartInfo.UseShellExecute = true;
process.StartInfo.Verb = "runas";

Do you know how to fix this?

Source: Windows Questions

LEAVE A COMMENT