2008-11-26 306 views

回答

6

这里http://www.mredkj.com/vbnet/vbnetmapdrive.html

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 

    <StructLayout(LayoutKind.Sequential)> _ 
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 Const ForceDisconnect As Integer = 1 
Public Const RESOURCETYPE_DISK As Long = &H1 

Public Function MapDrive(ByVal DriveLetter As String, ByVal UNCPath As String) As Boolean 

     Dim nr As NETRESOURCE 
     Dim strUsername As String 
     Dim strPassword As String 

     nr = New NETRESOURCE 
     nr.lpRemoteName = UNCPath 
     nr.lpLocalName = DriveLetter & ":" 
     strUsername = Nothing '(add parameters to pass this if necessary) 
     strPassword = Nothing '(add parameters to pass this if necessary) 
     nr.dwType = RESOURCETYPE_DISK 

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

     If result = 0 Then 
      Return True 
     Else 
      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 
+0

完美工作,开箱即用。使用链接中的代码,因为它在切割和粘贴时形成得更好。 – user38349 2008-11-27 13:20:33

3

解决方法之一是将网络文件夹映射到可用的驱动器号。你可以完成,使用Windows操作系统的命令:

System.Diagnostics.Process.Start("net.exe", "use K: \\Server\URI\path\here /USER:<username> <password>") 

只需更换你所需要的凭据的用户名和密码,并确保驱动器盘符是可用的。

要断开你可以叫

System.Diagnostics.Process.Start("net.exe", "use /delete K:") 
+0

发现在我的情况下,它是足够的,而不指定驱动器名称发出此命令'的Process.Start(“NET.EXE” ,“use \\ Server \ URI \ path \ here/USER:”)`。然后任何后续访问此路径的尝试都不会要求提供凭据。 – Monsignor 2015-02-03 13:40:22

相关问题