Process.Start never returns when UAC denied

醉酒当歌 提交于 2020-05-25 11:28:49

问题


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:


   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.



来源:https://stackoverflow.com/questions/23350175/process-start-never-returns-when-uac-denied

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!