如何通过常用的System.IO类访问网络驱动器?

Clé*_*ent 14 .net vb.net filesystems io network-drive

我的软件处理文件的多个操作,现在我已经完成了使用System.IO类编写相关函数.

我现在需要添加对网络驱动器的支持.使用映射非常有效(虽然Directory.GetFiles有点低,我不知道为什么),但我现在希望能够直接处理诸如的路径\\192.168.0.10\Shared Folder\MyDrive.有没有办法处理这种类型的路径,除了使用新生成的路径将驱动器安装到可用的驱动器号,然后卸载?

Jas*_*aty 24

您可以\\直接在路径中使用UNC路径(以其开头).但是,您必须考虑此连接的凭据,这可能是棘手的部分.

有几种方法:

  1. 如果远程系统位于同一个域中,或者域之间存在信任关系,并且您的程序正在运行的用户具有适当的访问权限,那么它将"正常工作".

  2. 您可以弹出并执行net use命令(通过Windows net.exe程序)以使用特定的用户名和密码建立连接.请注意,在用户会话中运行的任何程序都可以使用连接,而不仅仅是您的应用程序.完成后,使用该/DELETE命令删除连接.典型的语法是:net use \\computername\sharename password /USER:domain\username.

  3. 你可以P/Invoke WNetAddConnection2完成同样的事情,net use而不是炮轰net.exe.通过传递NULL lpLocalName,没有分配驱动器号,但用户名和密码将应用于通过UNC路径进行的后续访问.该WNetCancelConnection2功能可用于断开连接.

  4. 您可以LogonUser使用LOGON32_LOGON_NEW_CREDENTIALS标志进行P/Invoke ,然后进行模拟,以向您的线程添加其他远程凭据.与#2和#3不同,对用户整个会话的影响会有所限制.(在实践中,这很少有利于众所周知的WNetAddConnection2解决方案.)

以下是如何WNetAddConnection2从VB.NET 调用的示例.

Private Sub Test()
    Dim nr As New NETRESOURCE
    nr.dwType = RESOURCETYPE_DISK
    nr.lpRemoteName = "\\computer\share"
    If WNetAddConnection2(nr, "password", "user", 0) <> NO_ERROR Then
        Throw New Exception("WNetAddConnection2 failed.")
    End If
    'Code to use connection here.'
    If WNetCancelConnection2("\\computer\share", 0, True) <> NO_ERROR Then
        Throw New Exception("WNetCancelConnection2 failed.")
    End If
End Sub

<StructLayout(LayoutKind.Sequential)> _
Private Structure NETRESOURCE
    Public dwScope As UInteger
    Public dwType As UInteger
    Public dwDisplayType As UInteger
    Public dwUsage As UInteger
    <MarshalAs(UnmanagedType.LPTStr)> _
    Public lpLocalName As String
    <MarshalAs(UnmanagedType.LPTStr)> _
    Public lpRemoteName As String
    <MarshalAs(UnmanagedType.LPTStr)> _
    Public lpComment As String
    <MarshalAs(UnmanagedType.LPTStr)> _
    Public lpProvider As String
End Structure

Private Const NO_ERROR As UInteger = 0
Private Const RESOURCETYPE_DISK As UInteger = 1

<DllImport("mpr.dll", CharSet:=CharSet.Auto)> _
Private Shared Function WNetAddConnection2(ByRef lpNetResource As NETRESOURCE, <[In](), MarshalAs(UnmanagedType.LPTStr)> ByVal lpPassword As String, <[In](), MarshalAs(UnmanagedType.LPTStr)> ByVal lpUserName As String, ByVal dwFlags As UInteger) As UInteger
End Function

<DllImport("mpr.dll", CharSet:=CharSet.Auto)> _
Private Shared Function WNetCancelConnection2(<[In](), MarshalAs(UnmanagedType.LPTStr)> ByVal lpName As String, ByVal dwFlags As UInteger, <MarshalAs(UnmanagedType.Bool)> ByVal fForce As Boolean) As UInteger
End Function
Run Code Online (Sandbox Code Playgroud)