Jan Schreuder on .Net

.Net code samples, experiences, observations

View my professional profile on LinkedIn

Recent Posts

Tags

News

  • Inappropriate comments will be deleted at my discretion.

    The information and code samples in this weblog is provided "AS IS" without warranty of any kind, either expressed or implied, including but not limited to the merchantability and/or fitness for a particular purpose.

Community

Email Notifications

Tool suppliers

Tools

General

Microsoft

Favorite blogs

Archives

How to: Scan directories without recursion

Still on the subject of scanning directories . On my post at code project, Oleg Shilo replied with the following code which shows how you can scan directores without using recursion. Pretty smart code, although this way you don't exactly walk the directory tree, but gradually work your way down one level at a time. Couldn't resist posting it here:

VS 2005 version:

private void WalkDirectories()
{
    int index = 0;
    List<string> dirList = new List<string>();
    List<string> fileList = new List<string>();
 
    dirList.Add(Environment.CurrentDirectory);
 
    while (index < dirList.Count)
    {
        foreach (string dir in Directory.GetDirectories(dirList[index]))
            dirList.Add(dir);
 
        foreach (string file in Directory.GetFiles(dirList[index]))
            fileList.Add(file);
 
        index++;
    }
}

VS 2003 version:

private void WalkDirectories()
{
    int index = 0;
 
    ArrayList dirList = new ArrayList();
    ArrayList fileList = new ArrayList();
 
    dirList.Add(@"C:\");
 
    while (index < dirList.Count)
    {
        foreach (string dir in Directory.GetDirectories(dirList[index].ToString()))
            dirList.Add(dir);
 
        foreach (string file in Directory.GetFiles(dirList[index].ToString()))
            fileList.Add(file);
 
        index++;
    }
}
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Please add 2 and 1 and type the answer here: