2

I want a safe way to get all of the files nested under the user folder (nominally C:\Users\TheUsersName). The problem is that most of the folders there are reparse points, so just iterating/recursing over the folder's folders will not return those folders, and therefore not the files under them as well.

The simple but unwanted solutions would be either to

  1. hard code those folders. - never a good idea. What if in the future a new one is added or one removed.
  2. for the UserProfile folder only, retrieve the folders that any reparse points under it point to. - not safe - may become cyclic.

So to sum up: is there any safe way to get all files under the user folder including those nested under the reparse points there?

Since (judging by the comments) it seems like this isn't clear. I'll state this explicitly:

I don't want to get the targets of the reparse points. Not even only in the specific case of UserProfile. Because there might be one pointing to UserProfile itself or a folder that contains it. I want some list of the normal (though not 'real') folders that are there like 'Documents' (which I would assume would not point to a folder that contains the UserProfile and therefore would not become cyclic). Or some other clean solution.

11
  • 1
    the title states get all folders... but the content states a safe way to get all the files... what should I do now? Commented Sep 5, 2017 at 10:46
  • 1
    @ΦXocę웃Пepeúpaツ Well, I want the files, but the problem lies with the folders. But I see your point. I'm trying to think of a solution now.
    – ispiro
    Commented Sep 5, 2017 at 10:47
  • 1
    You can use Environment.UserFolder, insert it into FolderInfo and proceed from there. Is that what you meant?
    – Yaron
    Commented Sep 5, 2017 at 10:57
  • @Yaron You mean DirectoryInfo. The problem is that that folder's "folders" are not all real folders. From the docs "The .NET Framework methods such as GetFiles and GetDirectories will not return any subdirectories under a reparse point. ". (That's a good thing.)
    – ispiro
    Commented Sep 5, 2017 at 11:01
  • See: stackoverflow.com/questions/2302416/… and google query to get to that was: c# get folder reparse point
    – Sorceri
    Commented Sep 7, 2017 at 17:22

1 Answer 1

2
+50

A simple loop like that:

foreach (var dir in new DirectoryInfo(@"c:\users\smo").GetDirectories())
{
    .... 
}

should return all folders in c:\users\smo.

As a side, note, a folder can't be a reparse point. A reparse point is some optional binary data associated with a file or folder. That data has a type ("tag") that indicates what to do with this extra information. One type of reparse point is a "mount point", used for the mounted folders feature. There are also NTFS symbolic links that use reparse points. I think you were talking about symbolic links here.

9
  • The real question I think is how to avoid infinite recursion when you drill down into a folder like %USERPROFILE%. E.g. AppData\Local\Application Data\Application Data\Application Data\Application Data...
    – shurik
    Commented Sep 7, 2017 at 21:46
  • Thanks. (The system tells me I can't award the bounty yet.) In Windows 7, perhaps because of the "libraries", I would get exceptions when trying to get sub directories of some folders like documents. Now, it seems, I only get an error from "My Documents", and not from "Documents".
    – ispiro
    Commented Sep 7, 2017 at 21:46
  • @shurik Thanks. I originally thought that I'd have these recursion traps since I thought I'd need to go through reparse points. But since I don't - I just won't, and therefore the OS will take care of avoiding reparse points, as the docs say "The .NET Framework methods such as GetFiles and GetDirectories will not return any subdirectories under a reparse point. ".
    – ispiro
    Commented Sep 7, 2017 at 21:50
  • @ispiro - how do you get those exceptions in the first place? from the explorer UI? from, code this will also work new DirectoryInfo(@"c:\documents and settings\all users").GetDirectories() Commented Sep 7, 2017 at 22:02
  • I read the doc, not sure, possibly I am missing something. I create a junction and a quick POC in C# and for me it does return sub-directories under the junction. foreach (var d in Directory.GetDirectories(@"D:\Temp\tptest", "*", System.IO.SearchOption.AllDirectories)) { Console.WriteLine(d); } prints: D:\Temp\tptest\dir D:\Temp\tptest\junction D:\Temp\tptest\dir\dir2 D:\Temp\tptest\junction\dir2
    – shurik
    Commented Sep 7, 2017 at 22:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.