Process.Start never returns when UAC denied

2020-07-03 04:15发布

I have an updater exe that is meant to close the primary exe, replace it with an updated exe, and then launch that updated exe. When the updater attempts to start the updated exe, if the UAC permissions dialog is denied by the user, the updater will hang. This is because the Process.Start() function never returns. My CPU cycles meter indicates practically no usage btw.

I would hope all my users just say "yes" to the UAC, but since I'm here I'd like to handle this case with some kind of error message at least. Assume my users will have at least Windows 7. The exes themselves are 32 bit Winforms applications. Targeted .Net Framework is 4.0. Using Visual Studio 2010 Ultimate.

Any ideas on how to detect for when my user declines the UAC dialog?

I'm guessing all I can do is make the Process.Start() run on a separate thread that will timeout after a while. For more code:

private void RestartProcess()
{
    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.FileName = @"C:\Users\Me\Documents\Visual Studio 2010\Projects\updated.exe";
    MessageBox.Show("Attempting to start process");
    Process newProc = Process.Start(startInfo);
    MessageBox.Show("If this shows, the user has clicked YES in the UAC.");
}

Solution:

Process.Start()exits silently with a Win32Exception unless one uses a Try{}Catch{} block to catch the error.

1条回答
▲ chillily
2楼-- · 2020-07-03 05:01
   Process newProc = Process.Start(startInfo);
   MessageBox.Show("If this shows, the user has clicked YES in the UAC.");

This is normal, the exception that's raised by Process.Start() will bypass the MessageBox.Show() call. It is a Win32Exception for Windows error code 1223, ERROR_CANCELLED, "The operation was cancelled by the user".

Clearly you'll want to avoid swallowing exceptions here.

查看更多
登录 后发表回答