当前位置:  开发笔记 > 后端 > 正文

如何使用VB.NET打开受密码保护的共享网络文件夹?

如何解决《如何使用VB.NET打开受密码保护的共享网络文件夹?》经验,为你挑选了1个好方法。

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



1> Fredou..:

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

     _
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

推荐阅读
wurtjq
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有