如何访问通过正常System

如何访问通过正常System

本文介绍了如何访问通过正常System.IO类网络驱动器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的软件处理多重操作上的文件,现在我已经写完了相关功能,使用 System.IO 类。

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

我现在需要添加支持的网络驱动器。使用映射工作得非常好(虽然 Directory.GetFiles 是有点低,我不知道为什么),但现在我希望能够直接处理与如 \\ 192.168.0.10 \共享文件夹\ MyDrive 路径。有什么办法来处理这​​种类型的不是安装的​​驱动器的可用驱动器号,使用新生成的路径,然后卸载?

I now need to add support for network drives. Using a mapping works very well (although Directory.GetFiles is 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?

推荐答案

您可以使用UNC路径直接在你的路(与 \\ 启动)。但是,您必须考虑此连接,它可以是棘手的部分凭证。

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.

有几种方法:

  1. 如果远程系统是同一个域还是存在的域之间的信任关系,而程序运行的用户为具有适当的访问权限,它会只是工作。

  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".

您可以掏出执行 NET USE 命令(通过Windows NET.EXE 节目),以使与特定的用户名和密码的连接。请注意,连接是通过在用户的会话中运行任何程序,而不仅仅是你的应用程序中使用。使用 / DELETE 命令删除的连接,当你完成。典型的语法是: NET USE \\计算机名\共享名密码/用户:域\用户名

You can shell out and execute the net use command (through the Windows net.exe program) 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 /DELETE command to remove the connection when you are done. The typical syntax is: net use \\computername\sharename password /USER:domain\username.

您可以P / Invoke WNetAddConnection2 来完成同样的事情 NET USE 不脱壳而出,以 NET.EXE 。通过传递NULL作为 lpLocalName ,没有驱动器号分配,但用户名和密码将应用到后续的访问通过UNC路径进行。该 WNetCancelConnection2 函数可用于断开连接。

You can P/Invoke WNetAddConnection2 to accomplish the same thing as net use without 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 WNetCancelConnection2 function can be used to disconnect.

您可以P / Invoke 的LogonUser LOGON32_LOGON_NEW_CREDENTIALS 标志和随后的模拟来增加额外的远程凭据你的线程。与#2,#3,在用户的整个会议的影响将是一个多一点有限的。 (实际上,这是很少有利于知名 WNetAddConnection2 解决方案来完成。)

You can P/Invoke LogonUser with the LOGON32_LOGON_NEW_CREDENTIALS flag 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 WNetAddConnection2 solution.)

下面是如何调用 WNetAddConnection2 从VB.NET的样本。

The following is a sample of how to call WNetAddConnection2 from 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

这篇关于如何访问通过正常System.IO类网络驱动器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-21 01:52