简体   繁体   English

查找共享特殊文件夹(虚拟文件夹)中的所有文件

[英]Find all files in a shared special folder (Virtual Folders)

Im looking for a way to find all files in an shared special folder (Virtual Folder). 我正在寻找一种方法来查找共享的特殊文件夹(虚拟文件夹)中的所有文件。 The Desktop for example is an shared folder, there is a public Desktop for all users and a private Desktop . Desktop的例子是一个共享文件夹,有一个公共Desktop为所有用户和私人Desktop By navigating with the file explorer to Desktop you will see the contents of both desktops merged together. 通过使用文件浏览器导航到Desktop您将看到两个桌面的内容合并在一起。


Example: 例:

Shared folder for all: 所有共享文件夹:

dir C:\Users\Public\Desktop
Testfile1
Testfile2

Folder for the current user: 当前用户的文件夹:

dir C:\Users\usera\Desktop
Testfile3
Testfile4

Now I want to get all files from Testfile1 till Testfile4 by looping trough C:\\Users\\usera\\Desktop 现在我想通过循环槽C:\\Users\\usera\\DesktopTestfile1Testfile4获取所有文件

Someone has a clue howto get a list of the files of both directories merged together? 有人知道如何获取合并在一起的两个目录的文件列表? Also not only for Desktop, there are other folders that behave the same way. 同样,不仅对于桌面,还有其他文件夹的行为也相同。


Pseudocode: 伪代码:

arrayDesktop = FunctionThatGetsAllFilesFrom(@"C:\Usera\Desktop");
foreach (var file in arrayDesktop)
{
    Console.WriteLine(file);
}

this should now print out 现在应该打印出来

Testfile1
Testfile2
Testfile3
Testfile4

Use Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) and Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory) to get the files on your Desktop and the Public one respectively. 使用Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory)分别在您的桌面和公用文件夹上获取文件。

For other Virtual Folders you can look at the documentation . 对于其他虚拟文件夹,请参阅文档 But you still have to merge all the files yourself. 但是您仍然必须自己合并所有文件。

This isn't tested code so forgive any errors, but it should be enough to get you started. 这不是经过测试的代码,因此可以原谅任何错误,但足以让您入门。

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);
        }
    }
}

Unless you're running this as an administrator though, you're likely to run into security issues ie unable to read the directory. 除非您以管理员身份运行,否则您很可能会遇到安全问题,即无法读取目录。 In this instance, you're going to need the System.Net.NetworkCredential object and store the admin account in local cache - something like this. 在这种情况下,您将需要System.Net.NetworkCredential对象,并将管理帐户存储在本地缓存中-这样。

NetworkCredential credential = new NetworkCredential(username, password, domain);
CredentialCache credentialCache = new CredentialCache();
credentialCache.Add(new Uri(@"\\computer-uri"), "Basic", credential);

They are separate folders on the file system. 它们是文件系统上的单独文件夹。 Windows just combines them both to display on the desktop. Windows只是将它们结合在一起以显示在桌面上。 You are going to have to get all the files from both folders and combine them into a single list. 您将必须从两个文件夹中获取所有文件,并将它们合并到一个列表中。

You can get the list of files in a given folder with Directory.GetFiles . 您可以使用Directory.GetFiles获取给定文件夹中的文件列表。

Once you have the files from both folders, you can combine them with the Linq Concat extension method. 从两个文件夹中获取文件后,即可将它们与Linq Concat扩展方法结合使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM