我有一个小命令,允许我从 CMD 查询信息,但是它需要管理员权限(不是在我的应用程序级别,而是在 CMD 级别)。我走这条路线的唯一原因是因为我无法让 WMI 访问查询我一生的 Bitlocker 设置,这个项目需要离开我的办公 table 。
if (bitA.Text == "Bitlocker Available")
{
Process cmd2 = new Process();
cmd2.StartInfo.Verb = "runas";
cmd2.StartInfo.FileName = "cmd.exe";
cmd2.StartInfo.Arguments = "/c ping 8.8.8.8";
cmd2.StartInfo.UseShellExecute = false;
cmd2.StartInfo.RedirectStandardOutput = true;
cmd2.StartInfo.RedirectStandardError = true;
cmd2.Start();
//* Read the output (or the error)
string output2 = cmd2.StandardOutput.ReadToEnd();
bitB.Text = output2;
cmd2.WaitForExit();
}
最佳答案
您可以通过将 startInfo 对象的 Verb 属性设置为“runas”来指示应使用提升的权限启动新进程,如下所示:
startInfo.Verb = "runas";
这将导致 Windows 的行为就像是使用“以管理员身份运行”菜单命令从资源管理器启动该进程一样。系统将通过 UAC 提示用户确认他们想要执行此操作。
编辑:我发现您已经设置了该动词。你是问能不能绕过UAC?这有点愚蠢,否则病毒编写者等可以用一行代码绕过安全检查。
关于c# - 有没有办法通过 C# 命令以管理员身份打开 CMD?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30654985/