如何通过常用的 System.IO 类访问网络驱动器?
我的软件处理对文件的多个操作,现在我已经使用 System.IO 类完成了相关函数的编写。
我现在需要添加对网络驱动器的支持。使用映射效果很好(虽然 Directory.GetFiles
有点低,我不知道为什么),但我现在希望能够直接处理诸如 \\192.168.0.10\共享文件夹\MyDrive
。除了将驱动器安装到可用驱动器号、使用新生成的路径然后卸载之外,还有其他方法可以处理此类路径吗?
My software handles multiple operations on files, and I have now finished writing the related functions, using the System.IO
classes.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以直接在路径中使用 UNC 路径(以
\\
开头)。但是,您必须考虑此连接的凭据,这可能是棘手的部分。有几种方法:
如果远程系统位于同一域中或者域之间存在信任关系,并且您的程序运行的用户具有适当的访问权限,则它将“正常工作”。
您可以 shell 执行并执行
net use
命令(通过 Windowsnet.exe
程序),以使用特定的用户名和密码建立连接。请注意,连接可供用户会话中运行的任何程序使用,而不仅仅是您的应用程序。完成后,使用/DELETE
命令删除连接。典型的语法是:net use \\computername\sharename password /USER:domain\username
。您可以 P/Invoke
WNetAddConnection2
来完成与net use
相同的操作,而无需使用net.exe
。通过将 NULL 作为lpLocalName
传递,不会分配驱动器盘符,但用户名和密码将应用于通过 UNC 路径进行的后续访问。WNetCancelConnection2
函数可用于断开连接。您可以使用
LOGON32_LOGON_NEW_CREDENTIALS
标志 P/InvokeLogonUser
,然后进行模拟,以向您的线程添加其他远程凭据。与#2 和#3 不同,对用户整个会话的影响会更加有限。 (实际上,很少这样做,有利于众所周知的WNetAddConnection2
解决方案。)以下是如何从 VB.NET 调用
WNetAddConnection2
的示例。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.There are several approaches:
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".
You can shell out and execute the
net use
command (through the Windowsnet.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
.You can P/Invoke
WNetAddConnection2
to accomplish the same thing asnet use
without shelling out tonet.exe
. By passing NULL aslpLocalName
, no drive letter is assigned, but the username and password will apply to subsequent accesses made through the UNC path. TheWNetCancelConnection2
function can be used to disconnect.You can P/Invoke
LogonUser
with theLOGON32_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-knownWNetAddConnection2
solution.)The following is a sample of how to call
WNetAddConnection2
from VB.NET.使用普通的 UNC 路径(例如您提到的路径)非常适合我。例如:
工作得很好。如果没有,您可能遇到安全问题或其他问题。在这种情况下,您必须考虑冒充来解决这个问题。有关模拟的更多信息,请查看此。
Using normal UNC paths such as the one you mentioned works perfectly for me. For example:
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 this for more on impersonation.
您发布的 UNC 路径 (
\\192.168.0.10\Shared Folder\MyDrive
) 很奇怪。没有“驱动器”,这样的共享表现为目录。您可以使用Directory.GetFiles(@"\\192.168.0.10\共享文件夹")
。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 useDirectory.GetFiles(@"\\192.168.0.10\Shared Folder")
.