3

In my application, I need to run a batch file as admin for it to function.
I'm using this so far but I cant remember how to use the runas feature which allows it to run with admin rights.

process.start("filelocation.bat")

Any help would be apreciated.

GSerg
  • 73,524
  • 17
  • 153
  • 317
Chris Wilson
  • 49
  • 1
  • 1
  • 7
  • Possible duplicate of http://stackoverflow.com/questions/133379/elevating-process-privilege-programatically – Timiz0r Jul 16 '12 at 13:03

3 Answers3

3
Try
    Dim procInfo As New ProcessStartInfo()
    procInfo.UseShellExecute = True
    procInfo.FileName = (FileLocation)
    procInfo.WorkingDirectory = ""
    procInfo.Verb = "runas"
    Process.Start(procInfo)
Catch ex As Exception
    MessageBox.Show(ex.Message.ToString())
End Try
user1244772
  • 284
  • 4
  • 9
  • 22
1

You could try with this code:

Dim proc as ProcessStartInfo  = new ProcessStartInfo()
proc.FileName = "runas"
proc.Arguments = "/env /user:Administrator filelocation.bat"
proc.WorkingDirectory = "your_working_dir"
Process.Start(proc)

This code will ask the Administrator password and the start the execution of your batch file

EDIT: This is an alternative without the cmd window

Dim proc as ProcessStartInfo  = new ProcessStartInfo()
proc.FileName = "filelocation.bat"
proc.WorkingDirectory = "your_working_dir"  // <- Obbligatory
      proc.UseShellExecute = False
      proc.Domain = userDomain // Only in AD environments?
      proc.UserName = userName
      proc.Password = securePassword
Process.Start(proc)

It's a little more complicated because you need to get the input values (userName, Password, domain) before using this code and the password is a SecureString that you need to build in a dedicated way

Dim securePassword as New Security.SecureString()
For Each c As Char In userPassword
    securePassword.AppendChar(c)
Next c
Steve
  • 208,592
  • 21
  • 221
  • 278
  • Hi, This doesnt quite run as I would like. I need it to run as admin but first as the users permission in the small dialogue box, not through cmd. Thanks – Chris Wilson Jul 16 '12 at 12:10
  • Updated my answer. This alternative solution requires you get the inputs in some way from your user – Steve Jul 16 '12 at 13:34
0
    Imports System.Diagnostics
    Imports System.Security

'nice examples. just adding namespaces

DanCZ
  • 182
  • 3
  • 10