Directory::GetDirectories returning non-existent directories

P

Peteroid

Assuming you have the default directory settings, try this code:

String^ full_path = "c:\\Users\\Owner\\Documents" ;
array<String^>^ dir_list = Directory::GetDirectories( full_path ) ;

In my case, it returns all the directories as it should in dir_list, but it
also returns the following non-existent (hidden?) directories:

My Music
My Pictures
My Videos

If I try to Directory::GetFiles( ) on any of these directories it throws an
exception, but it will successfully return the files of those directories
which are visible or do exist.

If I do a 'search' for any directories with those names, they are not found.

I have a Windows Vista machine, and I'm programming in VS VC++ 2008 Express
(Beta 2) /cli pure.

What is going on here?
 
D

Doug Harrison [MVP]

Assuming you have the default directory settings, try this code:

String^ full_path = "c:\\Users\\Owner\\Documents" ;
array<String^>^ dir_list = Directory::GetDirectories( full_path ) ;

In my case, it returns all the directories as it should in dir_list, but it
also returns the following non-existent (hidden?) directories:

My Music
My Pictures
My Videos

If I try to Directory::GetFiles( ) on any of these directories it throws an
exception, but it will successfully return the files of those directories
which are visible or do exist.

If I do a 'search' for any directories with those names, they are not found.

I have a Windows Vista machine, and I'm programming in VS VC++ 2008 Express
(Beta 2) /cli pure.

What is going on here?

In a nutshell, they're junction points MS added to Vista to help existing
programs that did things the wrong way. This article explains all:

Junction Points and Backup Applications
http://msdn2.microsoft.com/en-us/library/bb756982.aspx
<q>
These junction points have file attributes of FILE_ATTRIBUTE_REPARSE_POINT
& FILE_ATTRIBUTE_SYSTEM and the ACL’s are set to “Everyone Deny Read”.
Applications must have permissions in order to call out and traverse a
specific path. However, enumerating the contents of these junction points
is not possible.
</q>

The article very helpfully lists all the junction points, including the
ones you encountered. Hopefully, there is a Directory::GetDirectories
overload that you can use to prevent it from returning these unwanted
junctions.

To examine the security attributes of these junction points, you'll have to
go to Folder Properties and disable the options that hide files. Then
you'll be able to see them in Documents, as well as several in the root of
your profile folder.
 
P

Peteroid

Thank you, Doug!

I haven't found a version of GetDirectories that doesn't return junction
points. So I screen out junction points by doing a 'GetFiles( )' on it
within a try-catch and screen out those that get caught.

But I do think a version of GetDirectories that doesn't report junction
points is really something MS should include. Any solution that requires
try-catch feels like a 'hack' to me.
 
D

Doug Harrison [MVP]

Thank you, Doug!

I haven't found a version of GetDirectories that doesn't return junction
points. So I screen out junction points by doing a 'GetFiles( )' on it
within a try-catch and screen out those that get caught.

But I do think a version of GetDirectories that doesn't report junction
points is really something MS should include. Any solution that requires
try-catch feels like a 'hack' to me.

I agree. Maybe you could filter by testing the attributes?
 
P

Peteroid

I agree. Maybe you could filter by testing the attributes?

Ah crap. I found the Directory class before the DirectoryInfo class (I
should have known better, I was already working eith DriveInfo and FileInfo
classes). I didn't realize I could get attributes since Directory class only
returns the names of the directories.

I did all of my runtime discovery of folders ala try-catch (e.g., that's how
I found out a directory was read-only). But I got 'distracted' with the
Directory class and never thought to look for DirectoryInfo class. Back to
the drawing board (System::Drawing class, for the record).

That was my first instinct. But GetDirectories( ) only returns an array of
the
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top