0

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. By navigating with the file explorer to Desktop you will see the contents of both desktops merged together.


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

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
Eun
  • 4,146
  • 5
  • 30
  • 51
  • For reference, the official name of these types of folders is [Virtual Folders](http://en.wikipedia.org/wiki/Special_folder#Virtual_folders). Not to be confused with the (identically named!) [Virtual Folders](http://en.wikipedia.org/wiki/Virtual_folder#Windows) – RB. Oct 23 '12 at 14:09

3 Answers3

0

They are separate folders on the file system. Windows just combines them both to display on the desktop. 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.

Once you have the files from both folders, you can combine them with the Linq Concat extension method.

cadrell0
  • 17,109
  • 5
  • 51
  • 69
  • I know that these are two seperate folders, the thing is that there is not only Desktop shared, also other folders. And I don't want to check them all. I wanted to know if there is a method that does the "merging" for me. – Eun Oct 23 '12 at 14:11
0

Use Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) and Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory) to get the files on your Desktop and the Public one respectively.

For other Virtual Folders you can look at the documentation. But you still have to merge all the files yourself.

Chrono
  • 1,433
  • 1
  • 16
  • 33
  • as I said before, I dont want only the Desktop, I want also the other `VirtualFolders` – Eun Oct 23 '12 at 14:16
  • You can find other Virtual Folder in the documentation. I've edited my answer to reflect this. – Chrono Oct 23 '12 at 14:32
0

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 i.e. 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.

NetworkCredential credential = new NetworkCredential(username, password, domain);
CredentialCache credentialCache = new CredentialCache();
credentialCache.Add(new Uri(@"\\computer-uri"), "Basic", credential);
Paul Aldred-Bann
  • 5,840
  • 4
  • 36
  • 55
  • That does not match the question. Your code works only for Desktop. – Eun Oct 23 '12 at 14:18
  • @Eun it seems others have made the same "mistake", your question is confusing. Perhaps if you provided some pseudo-code of some kind to show us what you're looking for? – Paul Aldred-Bann Oct 23 '12 at 14:20