【问题标题】:Process.Start never returns when UAC denied当 UAC 被拒绝时,Process.Start 永远不会返回
【发布时间】:2014-06-14 12:51:40
【问题描述】:

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

我希望我的所有用户都对 UAC 说“是”,但既然我在这里,我想至少用某种错误消息来处理这种情况。假设我的用户至少拥有 Windows 7。exe 本身是 32 位 Winforms 应用程序。目标 .Net 框架是 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.");
}

解决方案:

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

【问题讨论】:

  • 我假设(根据您上面指出的内容)您知道用户何时在 UAC 对话框上单击“是”,对吗?如果您知道这一点,为什么不使用DialogResult Property 来处理“否”的情况?
  • 您尝试修改Manifest 吗?甚至不给用户说不的机会。
  • 瓶颈是对 Process.Start() 的调用。当用户单击“是”时,一切正常。当用户单击“否”时,该函数永远不会返回,因此没有什么可处理的。
  • @Jason Goemaat 提供了更多代码。
  • 试试startInfo.Verb = "runas";

标签: c# winforms uac process.start


【解决方案1】:
   Process newProc = Process.Start(startInfo);
   MessageBox.Show("If this shows, the user has clicked YES in the UAC.");

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

显然,您需要避免在这里吞下异常。

【讨论】:

  • 完美。所需要的只是一个简单的 try catch 语句。谢谢帕桑特先生。
猜你喜欢
  • 2020-11-18
  • 2018-12-02
  • 2016-10-01
  • 1970-01-01
  • 2018-06-08
  • 2019-11-15
  • 2011-07-25
  • 1970-01-01
  • 2012-10-02
相关资源
最近更新 更多