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

时间:2014-07-17 01:49:13

标签: vb.net path

我有用于将驱动器映射到网络路径的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 个答案:

答案 0 :(得分: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),然后映射您的网络驱动器。