Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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
    Imports System.Diagnostics
    Imports System.Security

'nice examples. just adding namespaces

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top