【问题标题】:How do I run CMD.exe as administrator如何以管理员身份运行 CMD.exe
【发布时间】:2017-07-03 11:57:04
【问题描述】:

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

我如何以提升的权限运行(作为管理员)

谁能帮帮我?

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  Shell($"cmd.exe /c {TextBox1.Text} {TextBox2.Text}")
End Sub

【问题讨论】:

标签: vb.net


【解决方案1】:

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

改为使用ProcessStartInfoProcess.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

【讨论】:

  • 谢谢你!!!!!!!!!我在我的测试项目中使用了上述内容,并且效果很好。
  • @JeffreyRoccaforte :如果此答案解决了您的问题,请按帖子左侧的勾号/复选标记将其标记为“已接受”。欲了解更多信息,请参阅:How does accepting an answer work?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-25
  • 1970-01-01
  • 2010-10-14
  • 2012-11-22
  • 2016-09-20
  • 2010-11-26
  • 2012-05-28
相关资源
最近更新 更多