Jeffrey Roccaforte
Jeffrey Roccaforte

Reputation: 53

How do I run CMD.exe as administrator

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………….

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

Upvotes: 2

Views: 12000

Answers (1)

user7877793
user7877793

Reputation:

Running an application as Administrator using Shell() is not possible (unless the target file is set to always run with Administrator access).

Instead, use ProcessStartInfo and 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

Upvotes: 7

Related Questions