以编程方式映射网络驱动器

4
我有一段VB.NET代码,可以将驱动器映射到网络路径。
strPath = "\\11.22.33.11\Hostsw\Host\SW\"  

当我使用以下函数调用 MapDrive("T", strpath, "pcs", "$pcspcs$") 时,出现错误并显示消息 "Bad path could not connect to Star Directory"

Public Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" (ByRef lpNetResource As NETRESOURCE, ByVal lpPassword As String, ByVal lpUserName As String, ByVal dwFlags As Integer) As Integer
        Public Declare Function WNetCancelConnection2 Lib "mpr" Alias "WNetCancelConnection2A" (ByVal lpName As String, ByVal dwFlags As Integer, ByVal fForce As Integer) As Integer
        Public Const ForceDisconnect As Integer = 1
        Public Const RESOURCETYPE_DISK As Long = &H1
        Private Const ERROR_BAD_NETPATH = 53&
        Private Const ERROR_NETWORK_ACCESS_DENIED = 65&
        Private Const ERROR_INVALID_PASSWORD = 86&
        Private Const ERROR_NETWORK_BUSY = 54&

        Public Structure NETRESOURCE
            Public dwScope As Integer
            Public dwType As Integer
            Public dwDisplayType As Integer
            Public dwUsage As Integer
            Public lpLocalName As String
            Public lpRemoteName As String
            Public lpComment As String
            Public lpProvider As String
        End Structure
        Public Function MapDrive(ByVal DriveLetter As String, ByVal UNCPath As String, ByVal strUsername As String, ByVal strPassword As String) As Boolean


            Dim nr As NETRESOURCE

            nr = New NETRESOURCE
            nr.lpRemoteName = UNCPath
            nr.lpLocalName = DriveLetter & ":"
            nr.lpProvider = Nothing
            nr.dwType = RESOURCETYPE_DISK

            Dim result As Integer
            result = WNetAddConnection2(nr, strPassword, strUsername, 0)

            If result = 0 Then
                Return True
            Else
                Select Case result
                    Case ERROR_BAD_NETPATH
                        PostBackMessageHiddenField.Value = "QA4001I Bad path could not connect to Star Directory"
                    Case ERROR_INVALID_PASSWORD
                        PostBackMessageHiddenField.Value = "QA4002I Invalid password could not connect to Star Directory"
                    Case ERROR_NETWORK_ACCESS_DENIED
                        PostBackMessageHiddenField.Value = "QA4003I Network access denied could not connect to Star Directory"
                    Case ERROR_NETWORK_BUSY
                        PostBackMessageHiddenField.Value = "QA4004I Network busy could not connect to Star Directory"
                End Select
                Return False
            End If

        End Function

        Public Function UnMapDrive(ByVal DriveLetter As String) As Boolean
            Dim rc As Integer
            rc = WNetCancelConnection2(DriveLetter & ":", 0, ForceDisconnect)

            If rc = 0 Then
                Return True
            Else
                Return False
            End If 
        End Function
1个回答

5

研究解决方案首先需要查看您的方法的P/Invoke定义:

http://www.pinvoke.net/default.aspx/mpr.wnetaddconnection2
http://pinvoke.net/default.aspx/Structures/NETRESOURCE.html

并确保您的方法定义是正确的。例如,您使用了Integer而不是UInt32等。

一个快速而有效的解决方案是不要重复造轮子,而是使用每个Windows安装中都包含的net工具:

Module Module1
Public Sub MapDrive(ByVal DriveLetter As String, ByVal UNCPath As String, ByVal strUsername As String, ByVal strPassword As String)
    Dim p As New Process()
    p.StartInfo.FileName = "net.exe"
    p.StartInfo.Arguments = " use " & DriveLetter & ": " & UNCPath & " " & strPassword & " /USER:" & strUsername
    p.StartInfo.CreateNoWindow = True
    p.Start()
    p.WaitForExit()
End Sub

Sub Main()
    MapDrive("x", "\\FoosServer\FoosShare", "FoosServer\Bob", "correcthorsebatterystaple")
End Sub

End Module

这段代码的作用是运行net.exe(路径已经在PATH环境变量中,不需要包含路径),并传递正确的参数(例如net use x: \\Server\share password /USER:domain\Username)。然后,它会映射您的网络驱动器。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接