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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24793623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:51:37  来源:igfitidea点击:

Map Network Drive Programmatically

vb.netpath

提问by Kapil

I have VB.NET code for mapping a drive to a network path.

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

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

When I call MapDrive("T", strpath, "pcs", "$pcspcs$")using the below function it errors with the message "Bad path could not connect to Star Directory".

当我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

回答by Jens

The research solution would be to first look at the P/Invoke Definitions for your methods

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

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

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

and make sure your method definitions are correct. You for example use Integerinstead of UInt32and so on.

并确保您的方法定义正确。例如,您使用Integer代替UInt32等等。

A quick and dirty yet effective solution is to not reinvent the wheel and instead use the nettool included in every Windows installation:

一个快速、肮脏但有效的解决方案是不要重新发明轮子,而是使用net每个 Windows 安装中包含的工具:

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

What the code does, is it runs net.exe(path is included in the PATHenvironment variable, so no need to include it) with the proper arguments (e.g. net use x: \\Server\share password /USER:domain\Username), and this then maps your network drive.

代码的作用是运行net.exe(路径包含在PATH环境变量中,因此无需包含它)并带有适当的参数(例如net use x: \\Server\share password /USER:domain\Username),然后映射您的网络驱动器。