2008-11-26 51 views

Respuesta

6

encontrar aquí 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

Funcionado perfectamente, nada más sacarlo de la caja. Usó el código del enlace ya que formó mejor al cortar y pegar. – user38349

3

una solución sería asignar la carpeta de red a una letra de unidad disponible. Que podría lograr que el uso de los comandos del sistema operativo Windows:

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

Basta con sustituir el nombre de usuario y la contraseña con las credenciales que necesita y asegurarse de que la letra de unidad está disponible.

Para desconectarse, puede llamar

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

En mi caso fue suficiente para emitir este comando sin especificar un nombre de unidad 'Process.Start ("net.exe" , "use \\ Server \ URI \ path \ here/USER: ") '. Y luego, cualquier intento posterior de acceder a esta ruta no pedirá credenciales. – Monsignor

Cuestiones relacionadas