vb.net - 以编程方式映射网络驱动器

标签 vb.net path

我有 VB.NET 代码,用于将驱动器映射到网络路径。

strPath = "\\11.22.33.11\Hostsw\Host\SW\"  

当我使用以下函数调用 MapDrive("T", strpath, "pcs", "$pcspcs$") 时,会出现错误消息 “错误的路径无法连接到明星目录”

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

最佳答案

研究解决方案是首先查看方法的 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),然后这会映射您的网络驱动器。

关于vb.net - 以编程方式映射网络驱动器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24793623/

相关文章:

linux - 如何从 PHP (Apache) 调用 ejabberdctl

c# - 装箱在 C# 和 VB 中的行为不同

mysql - 如何在同一行的datagridview中插入2个不同的数组字符串

vb.net - 'tuition' 附近的语法不正确

php - Homebrew 软件在 Mac High Sierra 上的哪里安装 PHP?

c# - 在配置中为 Tracesource TextWriterTraceListener 输出添加自定义路径

.net - IsNumeric 在评估对象时抛出 FormatException

vb.net - ByVal 数据表操作

c# - 如何递归搜索具有多个通配符的目录?

python - 如何将文本转换为路径?