简体   繁体   English

以“以管理员身份”启动进程不起作用

[英]Start a process 'as administrator' is not working

I am using the following code within a program that does not have elevated privileges 我在没有提升特权的程序中使用以下代码

ProcessStartInfo pInfo = new ProcessStartInfo();
        pInfo.FileName = fileToExcecute;
        pInfo.UseShellExecute = false;
        pInfo.RedirectStandardOutput = false;
        pInfo.RedirectStandardError = false;
        pInfo.CreateNoWindow = true;
        if (runAsAdministrator)
          pInfo.Verb = "runas";

        Process p = Process.Start(pInfo);

The end user is asked to select whether they wish to run the program in elevated mode or not. 要求最终用户选择是否希望以提升模式运行程序。 The above however is not starting the program 'As Administrator' when runAsAdministrator is true . 但是,当runAsAdministrator为true时,上述方法不会启动程序“ As Administrator” I have ran the 'fileToExcute' manually 'As Administrator' and it prompts to make changes to the computer. 我已经以“以管理员身份”手动运行了“ fileToExcute”,它提示您对计算机进行更改。

I then added a manifest to the 'fileToExecute' to run with elevated privileges every time and when running that program directly I am properly prompted to confirm permission to make changes to the computer. 然后,我向“ fileToExecute”添加了清单,以便每次以提升的特权运行。当直接运行该程序时,系统会正确提示我确认对计算机进行更改的权限。 When I run the above program that uses the above code, I get: 当我运行使用上述代码的上述程序时,我得到:

    System.ComponentModel.Win32Exception (0x80004005):
 The requested operation requires elevation at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
    at System.Diagnostics.Process.Start() at System.Diagnostics.Process.Start(ProcessStartInfo startInfo) at
    #HLg.#ic.#zNg(String #ANg) in #pOg:line 135

I really want the first option to work. 我真的希望第一种选择有效。 I've tried everything and can't work out why the first option is not working. 我已经尝试了所有方法,但无法弄清为什么第一个选项不起作用。

You can't combine the Verb property with UseShellExecute = false , since verbs rely on that functionality. 您不能将Verb属性与UseShellExecute = false结合使用,因为动词依赖于该功能。 Set UseShellExecute to true and it should work. UseShellExecute设置为true使用。

I changed my code and did not use the pInfo configuration options. 我更改了代码,没有使用pInfo配置选项。 I just changed everything to p.verb = "runas" etc and it is now working. 我只是将所有内容更改为p.verb =“ runas”等,现在可以正常工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM