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
  • 5,576
  • 6
  • 37
  • 62
  • 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
  • 5,979
  • 5
  • 30
  • 54
  • THANK YOU!!!!!!!! I have used the above in my test project and it works perfectly. – Jeffrey Roccaforte Jul 04 '17 at 12:13
  • @JeffreyRoccaforte : If this answer solved your problem then please mark it as "accepted" by pressing the tick/check mark on the left of the post. For more information see: [**How does accepting an answer work?**](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – Visual Vincent Jul 06 '17 at 14:36