我正在寻找一种方法来查找共享特殊文件夹(虚拟文件夹)中的所有文件。
例如Desktop
是共享文件夹,所有用户都有公开Desktop
,私有Desktop
。通过将文件资源管理器导航到Desktop
,您将看到两个桌面的内容合并在一起。
示例:
所有人的共享文件夹:
dir C:\Users\Public\Desktop
Testfile1
Testfile2
当前用户的文件夹:
dir C:\Users\usera\Desktop
Testfile3
Testfile4
现在我希望通过Testfile1
Testfile4
到C:\Users\usera\Desktop
的所有文件
有人知道如何获取两个目录合并在一起的文件列表? 此外,不仅对于桌面,还有其他文件夹的行为方式相同。
伪代码:
arrayDesktop = FunctionThatGetsAllFilesFrom(@"C:\Usera\Desktop");
foreach (var file in arrayDesktop)
{
Console.WriteLine(file);
}
这应该打印出来
Testfile1
Testfile2
Testfile3
Testfile4
答案 0 :(得分:0)
使用Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
和Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory)
分别获取桌面和公共文件上的文件。
对于其他虚拟文件夹,您可以查看documentation。但你仍然必须自己合并所有文件。
答案 1 :(得分:0)
这不是经过测试的代码,所以原谅任何错误,但它应该足以让你开始。
foreach (string dir in Directory.GetDirectories(@"c:\Users"))
{
string fullDir = Path.Combine(dir, "Desktop");
if (Directory.Exists(fullDir))
{
foreach (string file in Directory.GetFiles(fullDir))
{
Console.WriteLine(file);
}
}
}
除非您以管理员身份运行此操作,否则您可能会遇到安全问题,即无法读取目录。在这种情况下,您将需要System.Net.NetworkCredential
对象并将管理员帐户存储在本地缓存中 - 就像这样。
NetworkCredential credential = new NetworkCredential(username, password, domain);
CredentialCache credentialCache = new CredentialCache();
credentialCache.Add(new Uri(@"\\computer-uri"), "Basic", credential);
答案 2 :(得分:-1)
它们是文件系统上的单独文件夹。 Windows只是将它们组合在一起以显示在桌面上。您将不得不从两个文件夹中获取所有文件并将它们合并到一个列表中。
您可以使用Directory.GetFiles
获取指定文件夹中的文件列表。
从两个文件夹中获取文件后,您可以将它们与Linq Concat
扩展方法结合使用。