start batch file from within vb.net as admin

2020-02-14 06:29发布

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.

标签: vb.net runas
3条回答
Rolldiameter
2楼-- · 2020-02-14 07:01
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
查看更多
家丑人穷心不美
3楼-- · 2020-02-14 07:07
    Imports System.Diagnostics
    Imports System.Security

'nice examples. just adding namespaces

查看更多
乱世女痞
4楼-- · 2020-02-14 07:08

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
查看更多
登录 后发表回答