Reputation: 20394
Windows 7 has a number of directories that have names from ancient times and cannot be accessed. I mean directories like "C:\Documents and Settings" and "C:\Dokumente und Einstellungen" where the real folder is "C:\Users". Another one is "C:\Programme" on localised systems. These directories show up when iterating all directories with Directory.GetDirectories
but cannot be accessed. When trying to access them, an UnauthorizedAccessException
is thrown. I could catch it, but it causes extra time that slows down my scan. I'd rather avoid looking into those fake directories in the first place. (I'm wondering why they're even there and whether I could just delete them, but that's another question.)
So what's a good way to detect such traps before actually falling into them? I guess it's some kind of link, but there's different kinds of links (and stuff from the guts of NTFS no worldly user can create themselves) and still I wouldn't know how to detect that from code.
I know that I still need to handle that exception, but if I already know in advance that an operation won't work I'd like to not do it to keep things fast. Disk scans are slow enough already.
My code is not supposed to run with administrative privileges, nor is it expecting to find anything useful in those linked directories.
Upvotes: 1
Views: 103
Reputation: 5084
If performance is your goal, you could do better than recursively iterating folders - you can read the MFT directly. It's not blissfully simple, but it is really fast. Here's a tutorial for doing the dirty work if you're interested.
Upvotes: 1
Reputation: 1963
DirectoryInfo.FileAttributes
contains the ReparsePoint
attribute in the case of a symbolic linked folder:
// Code from LinqPad, not sure it compiles in IDE:
DirectoryInfo di = new DirectoryInfo(@"c:\documents and settings");
Console.WriteLine("{0}", di.Attributes); // Hidden, System, Directory, ReparsePoint, NotContentIndexed
Upvotes: 2