当应用程序以管理员身份运行时,打开第二个表单时是否可能需要用户权限?

问题描述

我知道您可以创建一个清单文件来为整个应用程序指定访问级别管理员。但是是否可能只需要特定的形式?

解决方法

否,这是不可能的。

您可以做什么:让您的过程在没有提升的情况下运行。 当您发现需要升高时,但您的过程并未以更高的速度运行时,请使用“ runas”动词以及也许可以在新启动的过程中评估的某些命令行选项重新启动过程(Process.Start),以立即打开表单。

if (!RunningElevated())
{
    // restart as elevated process
    ProcessStartInfo psi = new ProcessStartInfo
    {
        UseShellExecute = true,Verb = "runas",WorkingDirectory = Environment.CurrentDirectory,FileName = Assembly.GetExecutingAssembly().Location,Argument = "--open MyForm" // has to be evaluated on the startup code
    };
    var process = Process.Start(psi);
    if (process != null)
        Application.Current.Shutdown(0); // this is for WPF
}