Ordering of directory info

M

Martin

Hi,

I have implemeted a recursive function that basically iterate throught the
entire file system and prints out the entire path of each directory that it
come across.

The only problem is the the directoryinfo objevy does not seem to list
directories in the same order as windows explorer would.
my main function is below but in order to demonstate my point here is a
senarion
image these are directories at the root of the C:/ drive

1 - level 2 - A
1 - level 2 - A
1 - level 2 - A
1 - level 2 - A
1 - level 2 - A
1 - level 2 - A
1 - level 2 - A
1 - level 2 - A
1 - level 2 - A
1 - level 2 - A




private static void IterateChildren(string dirRoot, StreamWriter stream)

{

try

{

DirectoryInfo info = new DirectoryInfo(dirRoot);

foreach (DirectoryInfo innerInfo in info.GetDirectories())

{

stream.WriteLine(innerInfo.FullName.ToString());

Console.WriteLine(innerInfo.FullName.ToString());

IterateChildren(innerInfo.FullName,stream);

}

}

catch (Exception ex)

{

Console.WriteLine(ex.Message);

}

}
 
M

Martin

Hi,

Please bear with this the previous message got sent before I had finished
typing.....

I have implemeted a recursive function that basically iterate throught
the
entire file system and prints out the entire path of each directory that it
come across.

The only problem is the the directoryinfo objevy does not seem to list
directories in the same order as windows explorer would.
my main function is below but in order to demonstate my point here is a
senarion
image there are directories at the root of the C:/ drive labelled

1 - level 2 - A
2 - level 2 - A
3 - level 2 - A
4 - level 2 - A
5 - level 2 - A
6 - level 2 - A
7 - level 2 - A
8 - level 2 - A
9 - level 2 - A
10 - level 2 - A
11 - level 2 - A

The list above is what windows exploer would show, however below is the list
that the directory info property will show when iterated through


1 - level 2 - A
10 - level 2 - A
11 - level 2 - A
2 - level 2 - A
3 - level 2 - A
3 - level 2 - A
4 - level 2 - A
5 - level 2 - A
6 - level 2 - A
7 - level 2 - A
8 - level 2 - A
9 - level 2 - A

can anybody explain why this is or how I would get the ordering the same in
the directory infor object.

cheers

martin
 
S

Scott Allen

Hi Martin:

DirectoryInfo will retrieve the information in the most effecient
manner it can. A program like Explorer will then sort the information
according to user preferences - size, date, name, etc.

You can sort just the way you need using IComparer. There is an
example in the documentation: "The following example demonstrates
listing the contents of a directory and sorting them by size. If the
directory has subdirectories, they are listed first." You'll find it
about half way down here:
http://msdn.microsoft.com/library/d...irectoryinfoclassgetfilesysteminfostopic1.asp
 

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