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.
Windows UAC - How to Handle the User Cancelling
-
11-10-2022 - |
Pergunta
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?
Solução
Outras dicas
You need to catch the error and if you use a background worker you won't get any exception error. Nevertheless, It is still bad practice not to catch exceptions separately.
Here is how It's done:
try
{
//your code here
using (process = new Process())
{
process.StartInfo.FileName = fileName;
process.StartInfo.Arguments = string.Join(" ", arg1, arg2, arg3);
process.StartInfo.Verb = "runas";
process.Start();
}
//end code
}
catch(System.ComponentModel.Win32Exception)
{
//Do Something or Leave it empty to swallow the exception
}