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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2352197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 14:00:07  来源:igfitidea点击:

How do I access a network drive through the usual System.IO classes?

.netvb.netfilesystemsionetwork-drive

提问by Clément

My software handles multiple operations on files, and I have now finished writing the related functions, using the System.IOclasses.

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

I now need to add support for network drives. Using a mapping works very well (although Directory.GetFilesis a bit low, and I don't know why), but I'd now like to be able to deal directly with paths such as \\192.168.0.10\Shared Folder\MyDrive. Is there any way to handle this type of paths other than mounting the drive to an available drive letter, using the newly generated path, and then unmounting?

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

回答by Jason Kresowaty

You can use the UNC path (which starts with \\) directly in your paths. However, you must account for the credentials for this connection, which can be the tricky part.

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

There are several approaches:

有几种方法:

  1. If the remote system is on the same domain or there is a trust relationship between the domains, and the user your program is running as has suitable access, it will "just work".

  2. You can shell out and execute the net usecommand (through the Windows net.exeprogram) to make a connection with a specific username and password. Be aware that connection is usable by any program running in the user's session, not just your application. Use the /DELETEcommand to remove the connection when you are done. The typical syntax is: net use \\computername\sharename password /USER:domain\username.

  3. You can P/Invoke WNetAddConnection2to accomplish the same thing as net usewithout shelling out to net.exe. By passing NULL as lpLocalName, no drive letter is assigned, but the username and password will apply to subsequent accesses made through the UNC path. The WNetCancelConnection2function can be used to disconnect.

  4. You can P/Invoke LogonUserwith the LOGON32_LOGON_NEW_CREDENTIALSflag followed by an impersonation to add additional remote credentials to your thread. Unlike #2 and #3, the effects on the user's entire session will be a little more limited. (In practice, this is rarely done in favor of the well-known WNetAddConnection2solution.)

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

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

  3. 您可以 P/InvokeWNetAddConnection2来完成与net use不使用net.exe. 通过将 NULL 作为 传递lpLocalName,不会分配驱动器号,但用户名和密码将应用于通过 UNC 路径进行的后续访问。该WNetCancelConnection2功能可用于断开连接。

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

The following is a sample of how to call WNetAddConnection2from VB.NET.

以下是如何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

回答by Steve Danner

Using normal UNC paths such as the one you mentioned works perfectly for me. For example:

使用正常的 UNC 路径(例如您提到的路径)非常适合我。例如:

string[] dirs = Directory.GetDirectories(@"\192.168.1.116\");

Works just fine. If it doesn't, you probably have a security issue or something. In which case, you'll have to look into impersonation to get around that. Check thisfor more on impersonation.

工作得很好。如果没有,您可能有安全问题或其他问题。在这种情况下,您必须研究模拟来解决这个问题。检查以获取有关模拟的更多信息。

回答by Hans Passant

The UNC path you posted (\\192.168.0.10\Shared Folder\MyDrive) is odd. There is no "drive", such a share behaves as a directory. You'd use Directory.GetFiles(@"\\192.168.0.10\Shared Folder").

您发布的 UNC 路径 ( \\192.168.0.10\Shared Folder\MyDrive) 很奇怪。没有“驱动器”,这样的共享就像一个目录。你会用Directory.GetFiles(@"\\192.168.0.10\Shared Folder").