简体   繁体   English

C#以管理员身份启动命令

[英]C# Launch command as administrator

I am trying to launch csript as an administrator (the account logged in has admin rights). 我正在尝试以管理员身份启动csript(登录的帐户具有管理员权限)。 setting the startinfo.verb to runas does not work. 将startinfo.verb设置为runas不起作用。

ProcessStartInfo p1 = new ProcessStartInfo();
p1.UseShellExecute = true;
p1.Verb = "runas";
p1.FileName = "cscript";
p1.Arguments = "I:\\WPKG\\wpkg.js /synchronize /quiet /nonotify";
Process p = new Process();
p.StartInfo = p1;
p.Start();

The only way I can get it to start with privileges is to manually set the username and password. 我可以通过权限启动它的唯一方法是手动设置用户名和密码。 However I cannot hardcode that information or put it into configurations. 但是,我无法对该信息进行硬编码或将其置于配置中。 Is there any way to have the cmd elevate without the login info? 有没有办法让cmd在没有登录信息的情况下提升?

I have also tried adding using (System.Security.Principal.WindowsIdentity.GetCurrent().Impersonate()) around the above code with no luck either. 我也试过在上面的代码周围添加使用(System.Security.Principal.WindowsIdentity.GetCurrent()。Impersonate())也没有运气。

Note: If I run the bat file directly, it works, if i run with password hardcoded, it works. 注意:如果我直接运行bat文件,它可以工作,如果我使用密码硬编码运行,它可以工作。 It only fails to elevate launching from C# without login information. 它只能在没有登录信息的情况下提升从C#的启动。

This utility: 这个工具:

http://jpassing.com/2007/12/08/launch-elevated-processes-from-the-command-line/ http://jpassing.com/2007/12/08/launch-elevated-processes-from-the-command-line/

may help. 可能有帮助。 Note too the comments in that post. 另请注意该帖子中的评论。

You have several choices: 你有几个选择:

  • Use the runas utility (not verb), and pass in the username and password to . 使用runas实用程序(不是动词),并将用户名和密码传递给。 You may need to do this manually once with saving the credentials (only good till a restart). 您可能需要手动执行此操作一次并保存凭据(仅在重新启动后才能执行此操作)。
  • Pass in the username and password on the ProcessStartInfo (you will need to convert the password to a SecureString . 传递ProcessStartInfo上的用户名和密码(您需要将密码转换为SecureString
  • Use the runas verb (not utility) to interact with UAC, as described in this question . 使用runas动词(非实用程序)与UAC交互,如本问题中所述

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

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