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

标签 .net vb.net networking

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

最佳答案

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

关于.net - 如何使用 VB.NET 打开受密码保护的共享网络文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/321128/

相关文章:

wpf - 将 CheckBox IsChecked 绑定(bind)到 Stackpanel 可见性

c++ - IOCP - 发布重叠或读取数据包?

java - 如何在我的 Android 应用程序中编写 StreamReader 程序?

.net - 使用 itextsharp 阅读 PDF,其中 PDF 语言为非英语

c# - COM+ 调用远程实例中的方法未知名称错误

mysql - 使用 MAX 函数连接 3 个表

c++ - 在 vb.net 应用程序中使用 C++ 头文件?

c# - 将复杂(嵌套)JSON 反序列化为 C# 对象

java - .NET、Java 到 JavaScript 编译器

linux - 已加载的 iptables 模块列表