I am unable to run batch file that is located in mapped network drive using System.Diagnostics.Process in VB.Net. I get:
'<batchfilename>.bat' is not recognized as an internal or external command, operable program or batch file.
Press any key to continue...
I get the same error when navigate to the folder (using explorer) that contains this batch file and double click it. I can successfully run the batch file by changing the directory to that folder through command prompt. I can't figure out why this is happening. I think the problem is Start() function of Process works like double click rather than just run the batch file. I have put my code below. Any help regarding this will be much appreciated.
Private WithEvents batchProcObj As New Process
Private Const SLEEP_AMOUNT As Integer = 100
Public Event Exited As EventHandler
Private eventHandled As Boolean
Public Sub startBatchProcess(ByVal batchProcFilePath As String, ByVal batchParams As String, ByVal domainName As String, ByVal loginName As String, ByVal passwd As String)
' Initialise process start info with batch process path and the parameters
Dim startInfoObj As New ProcessStartInfo()
Dim parentDir As New IO.DirectoryInfo(batchProcFilePath)
' Initialise event to false
eventHandled = False
' Get the batch file name without the extension
batchName = batchProcFilePath
Try
' Assign the batch process properties
startInfoObj.FileName = batchProcFilePath
startInfoObj.Arguments = batchParams
startInfoObj.UseShellExecute = False
startInfoObj.WindowStyle = ProcessWindowStyle.Normal
startInfoObj.WorkingDirectory = "C:\Windows\System32\"
startInfoObj.Domain = domainName
startInfoObj.UserName = loginName
startInfoObj.Password = appHandler.getSecureString(passwd)
startInfoObj.LoadUserProfile = True
batchProcObj.StartInfo = startInfoObj
batchProcObj.EnableRaisingEvents = True
' Start the batch file
batchProcObj.Start()
' Get the start time of the batch process
procStartTime = batchProcObj.StartTime
' Insert audit log
appHandler.writeAuditLog("RunBatchProcess", "START - Batch Process [" & batchName & "]")
Catch ex As Exception
appHandler.writeErrorLog("Start Batch Process: " & batchProcFilePath, ex)
End Try
' Continue running the batch process till it exits
Do While Not eventHandled
elapsedTime += SLEEP_AMOUNT
Thread.Sleep(SLEEP_AMOUNT)
Loop
End Sub