无法运行位于映射的网络驱动器上的批处理文件

时间:2011-11-29 21:53:14

标签: vb.net vb.net-2010

我无法使用VB.Net中的System.Diagnostics.Process运行位于映射网络驱动器中的批处理文件。我明白了:

'<batchfilename>.bat' is not recognized as an internal or external command, operable program or batch file.
Press any key to continue...

导航到包含此批处理文件的文件夹(使用资源管理器)并双击它时出现相同的错误。我可以通过命令提示符将目录更改为该文件夹来成功运行批处理文件。我无法弄清楚为什么会这样。我认为问题是Process的Start()函数就像双击而不只是运行批处理文件。我把我的代码放在下面。任何有关这方面的帮助将不胜感激。

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

1 个答案:

答案 0 :(得分:0)

只需使用FileIO.FileSystem并将其复制到临时文件中即可。 然后使用Backgroundworker运行。