Search code examples
c#.netwindowsexceptionuac

Windows UAC - How to Handle the User Cancelling


I need to spawn a process with elevated privileges which would cause a UAC prompt to pop up for the user:

using (process = new Process())
{
    process.StartInfo.FileName = fileName;
    process.StartInfo.Arguments = string.Join(" ", arg1, arg2, arg3);
    process.StartInfo.Verb = "runas";
    process.Start();
}

The problem that I'm having is that if the user selects 'no' in the UAC prompt, the process object will throw an exception stating that 'The operation was canceled by the user.' If the user selects no, then the parent process should just continue as normal (the spawned process is completely optional). Is there a better way of handling this scenario other than just catching the exception and doing nothing?


Solution

  • What else would you want to do instead of catching the exception and doing nothing? That is the only thing that I can think about.