471,328 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,328 software developers and data experts.

Recursive Reading : Corrected

The Folders looked all wrong with the formatting I gave thm... it could
cause confusion..so i'm reposting with a different fomat:
===================================

Hey everyone!

Basicaly what i'm doing is showing MessageBoxes with the contents of a
folder tree...

Example:

rootFolder (Folder1 "(" Folder2 *(* Folder3( F3File1, F3File2)
F2File1,F2File2* )* F1File1,F1File2 ")" RFFile1, RFFile2 )

It might take you a while but you'll probably see how the folder tree is
structured... the " * characters with the brackets are just to tell the
difference between them, reference.

RF"name" or F#"name" is to tell you in wich folder a file is, it's just for
reference, RFFile1,for example, is in the Root Folder, and F2File2 is in
Folder2

My problem is:

With the folowing code I can only get to the contents of
Folder2 which are Folder3, F2File1 etc etc...
but I can't get to the contents within Folder3.... could anyone suggest me
what to do? (I know it's quite simple, but i've been so many hours looking
at it that I can't see it!
protected void MessContents(DirectoryInfo root)
{

foreach (DirectoryInfo NextFolder in root.GetDirectories())
{
MessageBox.Show(NextFolder.Name);

foreach (FileInfo NextFile in root.GetFiles())
{
MessageBox.Show(NextFile.Name);
}

MessContents(NextFolder);
}
}

Thnx in advance!

Benjamin Vigneaux,

Jun 27 '08 #1
4 1116
Benjamin Vigneaux wrote:
The Folders looked all wrong with the formatting I gave thm... it could
cause confusion..so i'm reposting with a different fomat:
===================================

Hey everyone!

Basicaly what i'm doing is showing MessageBoxes with the contents of a
folder tree...

Example:

rootFolder (Folder1 "(" Folder2 *(* Folder3( F3File1, F3File2)
F2File1,F2File2* )* F1File1,F1File2 ")" RFFile1, RFFile2 )

It might take you a while but you'll probably see how the folder tree is
structured... the " * characters with the brackets are just to tell the
difference between them, reference.

RF"name" or F#"name" is to tell you in wich folder a file is, it's just for
reference, RFFile1,for example, is in the Root Folder, and F2File2 is in
Folder2

My problem is:

With the folowing code I can only get to the contents of
Folder2 which are Folder3, F2File1 etc etc...
but I can't get to the contents within Folder3.... could anyone suggest me
what to do? (I know it's quite simple, but i've been so many hours looking
at it that I can't see it!
protected void MessContents(DirectoryInfo root)
{

foreach (DirectoryInfo NextFolder in root.GetDirectories())
{
MessageBox.Show(NextFolder.Name);

foreach (FileInfo NextFile in root.GetFiles())
{
MessageBox.Show(NextFile.Name);
}

MessContents(NextFolder);
}
}

Thnx in advance!

Benjamin Vigneaux,
I don't see anything wrong with the code.

Why do you think that you can't get the contents of Folder3? I.E. what
happens when you try?

Have you verified that Folder3 actually contains anything?

--
Göran Andersson
_____
http://www.guffa.com
Jun 27 '08 #2
On Jun 14, 4:51 pm, "Benjamin Vigneaux" <b...@mi.clwrote:
The Folders looked all wrong with the formatting I gave thm... it could
cause confusion..so i'm reposting with a different fomat:
===================================

Hey everyone!

Basicaly what i'm doing is showing MessageBoxes with the contents of a
folder tree...

Example:

rootFolder (Folder1 "(" Folder2 *(* Folder3( F3File1, F3File2)
F2File1,F2File2* )* F1File1,F1File2 ")" RFFile1, RFFile2 )

It might take you a while but you'll probably see how the folder tree is
structured... the " * characters with the brackets are just to tell the
difference between them, reference.

RF"name" or F#"name" is to tell you in wich folder a file is, it's just for
reference, RFFile1,for example, is in the Root Folder, and F2File2 is in
Folder2

My problem is:

With the folowing code I can only get to the contents of
Folder2 which are Folder3, F2File1 etc etc...
but I can't get to the contents within Folder3.... could anyone suggest me
what to do? (I know it's quite simple, but i've been so many hours looking
at it that I can't see it!

protected void MessContents(DirectoryInfo root)
{

foreach (DirectoryInfo NextFolder in root.GetDirectories())
{
MessageBox.Show(NextFolder.Name);

foreach (FileInfo NextFile in root.GetFiles())
{
MessageBox.Show(NextFile.Name);
}

MessContents(NextFolder);
}

}

Thnx in advance!

Benjamin Vigneaux,

Not sure but

protected void MessContents(DirectoryInfo root)
{

// when root =Folder3 , There are are not directories

foreach (DirectoryInfo NextFolder in
root.GetDirectories())
{
MessageBox.Show(NextFolder.Name);

foreach (FileInfo NextFile in root.GetFiles())
{
MessageBox.Show(NextFile.Name);
}

MessContents(NextFolder);
}

}

I think this could help. not tested it though

foreach (FileInfo fi in di.GetFiles())
{
if ((fi.Attributes & FileAttributes.Directory) !=
FileAttributes.Directory)
{
//is file
}
else if ((fi.Attributes & FileAttributes.Directory) ==
FileAttributes.Directory)
{
//recurse
}
}
Jun 27 '08 #3
parez wrote:
// when root =Folder3 , There are are not directories
Good point. That is the problem.

However, the simple solution is to just move the file loop outside the
folder loop:

protected void MessContents(DirectoryInfo root) {
foreach (FileInfo NextFile in root.GetFiles()) {
MessageBox.Show(NextFile.Name);
}
foreach (DirectoryInfo NextFolder in root.GetDirectories()) {
MessageBox.Show(NextFolder.Name);
MessContents(NextFolder);
}
}
--
Göran Andersson
_____
http://www.guffa.com
Jun 27 '08 #4
On Jun 15, 4:57 am, Göran Andersson <gu...@guffa.comwrote:
parez wrote:
// when root =Folder3 , There are are not directories

Good point. That is the problem.

However, the simple solution is to just move the file loop outside the
folder loop:

protected void MessContents(DirectoryInfo root) {
foreach (FileInfo NextFile in root.GetFiles()) {
MessageBox.Show(NextFile.Name);
}
foreach (DirectoryInfo NextFolder in root.GetDirectories()) {
MessageBox.Show(NextFolder.Name);
MessContents(NextFolder);
}

}

--
Göran Andersson
_____http://www.guffa.com
hehe.. I missed that.. I was watching semi-pro when i replied to that
post. Will ferrel's worst movie ever
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Kaptain524 | last post: by
2 posts views Thread by Xavier Decoret | last post: by
6 posts views Thread by Johan Bergman | last post: by
1 post views Thread by Varun Kacholia | last post: by
5 posts views Thread by Seong-Kook Shin | last post: by
11 posts views Thread by randomtalk | last post: by
6 posts views Thread by Uwe Grawert | last post: by
reply views Thread by Kim G | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.