start batch file from within vb.net as admin

旧时模样 提交于 2019-11-29 15:18:45
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
    Imports System.Diagnostics
    Imports System.Security

'nice examples. just adding namespaces

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!