Windows UAC - 如何处理用户取消

问题描述 投票:0回答:2

我需要使用提升的权限生成进程,这会导致为用户弹出UAC提示:

using (process = new Process())
{
    process.StartInfo.FileName = fileName;
    process.StartInfo.Arguments = string.Join(" ", arg1, arg2, arg3);
    process.StartInfo.Verb = "runas";
    process.Start();
}

我遇到的问题是,如果用户在UAC提示中选择“否”,则进程对象将抛出一个异常,指出“操作已被用户取消”。如果用户选择“否”,则父进程应该正常继续(生成的进程完全是可选的)。除了捕获异常并且什么都不做之外,还有更好的方法来处理这种情况吗?

c# .net windows exception-handling uac
2个回答
2
投票

你还想做什么而不是捕捉异常而什么都不做?这是我唯一能想到的。


1
投票

您需要捕获错误,如果您使用后台工作程序,则不会出现任何异常错误。然而,不分别捕捉异常仍然是不好的做法。

以下是它的完成方式:

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
}
© www.soinside.com 2019 - 2024. All rights reserved.