vb.net 如何以管理员身份运行 CMD.exe
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44884983/
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
How do I run CMD.exe as administrator
提问by Jeffrey Roccaforte
Have been successful in running a DOS command using the contents of 2 text boxes. The issue that I am trying to find a resolution for is………….
已成功使用 2 个文本框的内容运行 DOS 命令。我试图找到解决方案的问题是…………。
How do I run with elevated rights (as an Administrator)
我如何以提升的权限运行(作为管理员)
Can anyone help me?
谁能帮我?
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Shell($"cmd.exe /c {TextBox1.Text} {TextBox2.Text}")
End Sub
回答by
Running an application as Administrator using Shell() is not possible (unless the target file is set to always run with Administrator access).
使用 Shell() 以管理员身份运行应用程序是不可能的(除非目标文件设置为始终以管理员访问权限运行)。
Instead, use ProcessStartInfoand Process.Start():
相反,使用ProcessStartInfo和Process.Start():
Dim psi As New ProcessStartInfo()
psi.Verb = "runas" ' aka run as administrator
psi.FileName = "cmd.exe"
psi.Arguments = "/c " & TextBox1.Text & TextBox2.Text ' <- pass arguments for the command you want to run
Try
Process.Start(psi) ' <- run the process (user will be prompted to run with administrator access)
Catch
' exception raised if user declines the admin prompt
End Try

