远程共享文件夹和驱动器 C# [英] Remote Shared folders and drives C#
问题描述
我想通过网络访问远程机器上的驱动器/文件夹/子文件夹/文件,以便使用 C# 的机器.
I want to access drives/folders/sub-folders/files on remote machines over network for a machine in C#.
我知道为此使用 WMI.但是,由于安全权限,WMI 可能无法在远程计算机上执行.
I know of a using WMI for this. However WMI might not be executable over remote machines due to security permissions.
在 C# 中枚举远程计算机的文件夹/子文件夹/文件的替代方法是什么.
What are alternative way in C# to enumerate the folders/subfolders/files for remote machines over the network.
谢谢!加根
推荐答案
可以像枚举本地目录一样枚举 UNC 路径上的共享文件夹,使用 System.IO 中的类 命名空间,例如Directory.EnumerateFiles
.
Shared folders on a UNC path can be enumerated just like local directories, using the classes in the System.IO namespace, like Directory.EnumerateFiles
.
foreach (var file in Directory.EnumerateFiles(@"\\machine\c$"))
{
}
但是,正如您所说,存在一个凭据问题.如果您需要指定不同的凭据来访问共享,则必须针对远程计算机进行身份验证.幸运的是,这个答案 用于创建临时网络连接:
However, as you say, there's a question of credentials. If you need to specify different credentials to access the shares, you'll have to authenticate against the remote machine. Luckily, there's a great solution in this answer for creating ad-hoc network connections:
using (new NetworkConnection("\\machine\c$", new NetworkCredential("username", "password")))
{
foreach (var file in Directory.EnumerateFiles(@"\\machine\c$"))
{
}
}
这篇关于远程共享文件夹和驱动器 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!