2

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
InteXX
  • 6,135
  • 6
  • 43
  • 80
  • possible duplicate: `How do I force my .NET application to run as administrator?' https://stackoverflow.com/questions/2818179/how-do-i-force-my-net-application-to-run-as-administrator – user1234433222 Jul 03 '17 at 12:00

1 Answers1

7

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
CrazyTim
  • 6,695
  • 6
  • 34
  • 55