2014-07-17 4 views
3

드라이브에 네트워크 경로를 매핑하는 VB.NET 코드가 있습니다. 네트워크 드라이브 프로그래밍 방식으로

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

라는 메시지가 "Bad path could not connect to Star Directory"로 아래의 기능이 오류를 사용하여 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 

답변

3

연구 솔루션은 방법
http://pinvoke.net/default.aspx/Structures/NETRESOURCE.html

http://www.pinvoke.net/default.aspx/mpr.wnetaddconnection2

의 P/호출 정의에서 첫번째보기로하고 당신의 방법 정의가 올바른지 확인합니다. 예를 들어 UInt32 대신 Integer을 사용하면됩니다.

코드가 무엇을
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 (경로에 포함되어 실행 :

신속하고 더러운하지만 효과적인 솔루션은 바퀴를 재발견하고 대신 net 도구는 모든 Windows 설치에 포함 된 사용하지 않는 것입니다 PATH 환경 변수이므로 적절한 인수 (예 : net use x: \\Server\share password /USER:domain\Username)와 함께 포함 시키십시오. 그러면 네트워크 드라이브가 매핑됩니다.

관련 문제