I am working on a windows application in VS2005 C#. The problem is that
I will read a directory for files. I have a foreach so I can loop al
the files in the directory. Know I wil do the same but for the
subdirectories also. So I think there must be a second foreach and
place the subdir in the method. Someone who can help me??
Here is some test code:
public void test(String path)
{
DirectoryInfo root = new DirectoryInfo(path);
DirectoryInfo[] dirs = root.GetDirectories();
String bla = "";
foreach (DirectoryInfo subDir in dirs)
{
bla += subDir.Name.ToString()+"-----";
}
richTextBox1.Text = bla;
}
Loading
Jan MontanoPosted Oct 8, 2007, 9:34 PM
public void test(String path) { DirectoryInfo root = new DirectoryInfo(path); DirectoryInfo[] dirs = root.GetDirectories("*", SearchOption.AllDirectories); String bla = ""; foreach (DirectoryInfo subDir in dirs) { bla += subDir.FullName.ToString() + Environment.NewLine;
FileInfo[] files = directory.GetFiles();
foreach (FileInfo file in files)
{
Console.WriteLine(file.FullName);
}
} rtb1.Text = bla; }
if you want to get the files in the root for processing as well, use root.GetFiles() before the loop
khalid JohnsonPosted Oct 6, 2007, 11:37 AM
Thanks for your support! I have only one problem and that is, that I will do something with all the files in the dir and subdirs. Know it is so that I only can do somenthing with the directories. So can you please explain that?
Scott LyslePosted Oct 6, 2007, 3:06 AM
Hello:
Use the search option for GetDirectories:
public void test(String path) { DirectoryInfo root = new DirectoryInfo(path); DirectoryInfo[] dirs = root.GetDirectories("*", SearchOption.AllDirectories); String bla = ""; foreach (DirectoryInfo subDir in dirs) { bla += subDir.FullName.ToString() + Environment.NewLine; } rtb1.Text = bla; }