vb.net 使用管理员权限运行 cmd.exe
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17843457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running cmd.exe with admin priviliges
提问by user2550788
This is my code trying to run cmd.exe with admin priviligies. However, I get the request operation requires elevation. if I run cmd.exe with "Run as Admin" through my windows, it works, however, through vb, it doesn't. This is my code.
这是我尝试以管理员权限运行 cmd.exe 的代码。但是,我得到请求操作需要提升。如果我通过我的 Windows 使用“以管理员身份运行”运行 cmd.exe,它会工作,但是,通过 vb,它不会。这是我的代码。
Try
Dim process As New Process()
process.StartInfo.FileName = "cmd.exe "
process.StartInfo.Verb = "runas"
process.StartInfo.UseShellExecute = False
process.StartInfo.RedirectStandardInput = True
process.StartInfo.RedirectStandardOutput = True
process.StartInfo.RedirectStandardError = True
process.StartInfo.CreateNoWindow = True
process.Start()
process.StandardInput.WriteLine("route add 8.31.99.141 mask 255.255.255.255 " & cmdorder)
process.StandardInput.WriteLine("exit")
Dim input As String = process.StandardOutput.ReadToEnd
process.Close()
Dim regex As Regex = New Regex("(ok)+", RegexOptions.IgnoreCase) ' wa requested
' txtLog.AppendText(input)
Return regex.IsMatch(input)
Thanks.
谢谢。
回答by Ian Boyd
You cannot achieve what you want.
你无法实现你想要的。
You canuse Process.Start()
to launch an elevated process, but onlyif you UseShellExecute = true
:
您可以使用Process.Start()
启动提升的过程中,但只有当你UseShellExecute = true
:
Dim process As New Process()
process.StartInfo.FileName = "cmd.exe "
process.StartInfo.Verb = "runas"
process.StartInfo.UseShellExecute = True
process.Start()
The reason is because you mustuse ShellExecute
if you want to launch an elevated process. Only ShellExecute
knows how to elevate.
原因是因为如果要启动提升的进程,则必须使用ShellExecute
。只ShellExecute
知道如何提升。
If you specify UseShellExecute = False
, then CreateProcess
is used rather than ShellExecute
. CreateProcess
doesn't know how to elevate. Why? From the AppCompat guy:
如果指定UseShellExecute = False
,则CreateProcess
使用 而不是ShellExecute
。CreateProcess
不知道怎么提升。为什么?来自 AppCompat 的家伙:
Well, CreateProcess is really low in the layers. What can you do without the ability to create a process? Not a whole lot. Elevation, however, is a different story. It requires a trip to the app elevation service. This then calls into consent.exe, which has to know how to read group policy and, if necessary, switch to the secure desktop and pop open a window and ask the user for permission / credentials, etc. We don't even need to take all of these features, let's just take the dialog box.
Now, for creating a process that requires elevation, normally you just switch up APIs. The shell sits in a much higher layer, and consequently is able to take a dependency on elevation. So, you'd just swap out your call to CreateProcess with a call to ShellExecute.
好吧, CreateProcess 在层中真的很低。如果没有创建流程的能力,你能做什么?不是很多。然而,海拔是另一回事。它需要访问应用程序提升服务。然后调用consent.exe,它必须知道如何读取组策略,并在必要时切换到安全桌面并弹出一个窗口并要求用户提供权限/凭据等。我们甚至不需要获取所有这些功能,让我们只获取对话框。
现在,要创建需要提升的流程,通常只需切换 API。外壳位于更高的层中,因此能够依赖于海拔。因此,您只需将您对 CreateProcess 的调用替换为对 ShellExecute 的调用。
So that explains how you can elevate cmd
, but once you do: you're not allowed to redirect output, or hide the window; as only CreateProcess
can do that:
这解释了如何提升cmd
,但是一旦你这样做了:你不能重定向输出,或者隐藏窗口;因为只能CreateProcess
这样做:
Redirecting I/O and hiding the window can only work if the process is started by CreateProcess().
只有当进程由 CreateProcess() 启动时,重定向 I/O 和隐藏窗口才能起作用。
Which was a long way of saying that this guy asked the same question over here; but without the indignity of having someone close your question.
这家伙在这里问了同样的问题,这是一个很长的说法;但没有让某人结束你的问题的侮辱。
Note: Any code is released into the public domain. No attribution required.
注意:任何代码都发布到公共领域。不需要归属。
回答by Mrk Hawes
Make it as an object and then set it to start up requiring admin priv in the application.object.settings thing.
将其设置为一个对象,然后将其设置为启动时需要在 application.object.settings 中使用 admin priv。