In my VB-NET application I need to run a batch file as administrator.
So far I use this code but can't remember how to use runas
:
process.start("filelocation.bat")
Any help is apreciated.
In my VB-NET application I need to run a batch file as administrator.
So far I use this code but can't remember how to use runas
:
process.start("filelocation.bat")
Any help is apreciated.
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
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