无法通过网络调用批处理文件

时间:2014-11-18 14:05:04

标签: vb.net batch-file

我正在尝试调用网络驱动器中的bat文件。我认为这行代码导致了这个问题:

Call Shell(Environ$("COMSPEC") & " /c \\filepath\hello.bat", vbNormalFocus)

这在我自己的机器上运行良好,但是一旦我进入网络就无法运行。是否与 / c 位有关?

2 个答案:

答案 0 :(得分:0)

您的shell环境中是否定义了网络路径?您可能需要使用完整的UNC路径。

答案 1 :(得分:0)

使用此代码,您可以从系统中的驱动器获取unc路径信息。

Sub SampleUNC()


    Dim uncPaths As Dictionary(Of String, String)
    Dim infoUnc As String = String.Empty

    'Get Unc Drives info in your system
    uncPaths = GetUncDrivePaths()

    For Each kvp As KeyValuePair(Of String, String) In uncPaths

        'X:   \\MyServer\Software\
        'Y:   \\MyServer\Documents\

        Debug.WriteLine(kvp.Key & "  " & kvp.Value)

    Next

    'get the Unc path from the mapped drive   \\MyServer\Documents\
    infoUnc = GetUncPath("Y:")

    MsgBox(infoUnc)

End Sub


Public Shared Function GetUncDrivePaths() As Dictionary(Of String, String)

    Dim uncDictionary As New Dictionary(Of String, String)

    Try

        Dim dis As DriveInfo() = DriveInfo.GetDrives()

        For Each di As DriveInfo In dis
            If di.DriveType = DriveType.Network Then

                Dim dir As DirectoryInfo = di.RootDirectory

                Dim name As String = dir.FullName.Substring(0, 2)
                Dim realPath As String = GetUNCPath(name)

                uncDictionary.Add(name, realPath)

                ' "x:"

                'MessageBox.Show(GetUNCPath(dir.FullName.Substring(0, 2)))

            End If
        Next

    Catch ex As Exception
        Throw
    End Try

    Return uncDictionary

End Function

Public Shared Function GetUncPath(ByVal path As String) As String

    Try

        If path.StartsWith("\\") Then
            Return path
        End If

        Dim mo As New ManagementObject()
        mo.Path = New ManagementPath(String.Format("Win32_LogicalDisk='{0}'", path))

        'DriveType 4 = Network Drive
        If Convert.ToUInt32(mo("DriveType")) = 4 Then
            Return Convert.ToString(mo("ProviderName"))
        Else
            Return path
        End If

    Catch ex As Exception

        Throw

    Finally


    End Try


End Function