【问题标题】:How to open a password protected shared network folder using VB.NET?如何使用 VB.NET 打开受密码保护的共享网络文件夹?
【发布时间】:2008-11-26 15:37:40
【问题描述】:

我需要在网络上打开一个受密码保护的共享文件夹才能访问 Access 97 数据库。如何打开文件夹并输入密码?

【问题讨论】:

    标签: .net vb.net networking


    【解决方案1】:

    在这里找到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
    

    【讨论】:

    • 完美运行,开箱即用。使用链接中的代码,因为它在剪切和粘贴时格式更好。
    【解决方案2】:

    一种解决方案是将网络文件夹映射到可用的驱动器号。您可以使用 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:" )
    

    【讨论】:

    • 就我而言,在不指定驱动器名称Process.Start("net.exe", "use \\Server\URI\path\here /USER:&lt;username&gt; &lt;password&gt;" ) 的情况下发出此命令就足够了。然后任何后续访问此路径的尝试都不会要求提供凭据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-11
    • 2019-09-17
    • 2010-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多