如何以管理员身份运行CMD.exe

Jef*_*rte 1 vb.net

已成功使用2个文本框的内容运行DOS命令。我正在尝试解决的问题是…………。

如何以提升的权限运行(以管理员身份)

谁能帮我?

私有子Button1_Click(作为对象发送,作为EventArgs发送)处理Button1.Click Shell(“ cmd.exe / c” + TextBox1.Text + TextBox2.Text)结束子

小智 5

不能使用Shell()以管理员身份运行应用程序(除非将目标文件设置为始终以管理员权限运行)。

而是使用ProcessStartInfoand 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
Run Code Online (Sandbox Code Playgroud)