当UAC被拒绝时,Process.Start永远不会返回

Chr*_*ell 13 c# uac process.start winforms

我有一个updater exe,旨在关闭主exe,用更新的exe替换它,然后启动更新的exe.当updater尝试启动更新的exe时,如果用户拒绝UAC权限对话框,则更新程序将挂起.这是因为Process.Start()函数永远不会返回.我的CPU周期表显示几乎没有使用btw.

我希望我的所有用户只对UAC说"是",但由于我在这里,我想至少用某种错误信息来处理这种情况.假设我的用户至少拥有Windows 7.这些exes本身就是32位Winforms应用程序.有针对性的.Net Framework是4.0.使用Visual Studio 2010 Ultimate.

有关如何检测用户何时拒绝UAC对话框的任何想法?

我猜我所能做的就是让Process.Start()一个单独的线程运行一段时间后会超时.有关更多代码:

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.");
}
Run Code Online (Sandbox Code Playgroud)

解:

Process.Start()除非使用Try {} Catch {}块来捕获错误,否则使用Win32Exception静默退出.

Han*_*ant 8

   Process newProc = Process.Start(startInfo);
   MessageBox.Show("If this shows, the user has clicked YES in the UAC.");
Run Code Online (Sandbox Code Playgroud)

这是正常的,Process.Start()引发的异常将绕过MessageBox.Show()调用.它是Windows错误代码1223的Win32Exception,ERROR_CANCELLED,"操作已被用户取消".

显然,你会想避免在这里吞咽异常.