By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,669 Members | 2,546 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,669 IT Pros & Developers. It's quick & easy.

Creating same new DirectoryInfo in loop?

P: n/a
I am recursing through ALL folders and sub-folders below a certain level to list all the files of a certain type in those folders. I use two ArrayLists, alFiles and alFolders, to track matching files and my progress in the recursion.

I use a while loop to manage the folder-subfolder drill-down. Each pass through the while loop creates a new DirectoryInfo object named currentFolder.

Is there are problem with that? Does this create multiple objects or does it destroy the existing DirectoryInfo object and create a new one?

Here is the relevant source:
ArrayList alFiles = new ArrayList();
ArrayList alFolders = new ArrayList();
FileInfo[] files;

alFolders.Add(rootPath);
while(alFolders.Count > 0)
{
DirectoryInfo currentFolder = new DirectoryInfo(alFolders[0].ToString());
folders = currentFolder.GetDirectories();
foreach(DirectoryInfo subFolder in folders)
alFolders.Add(currentFolder.FullName + "\\" + subFolder.Name);

files = currentFolder.GetFiles();
foreach(FileInfo file in files)
{
if (file.Name.EndsWith("xml"))
alFiles.Add(file.Name);
}

alFolders.RemoveAt(0);
}

Nov 16 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
I do not see a problem with that. The old objects will be destroyed when
garbage collection happens (as long as no other references exist, which it
looks like there is not).

As a side not, you might get cleaner code using a Queue object (not that
anything is wrong with your code, just an observation), that way you can
just Enqueue and Dequeue your items.

"Vagabond Software" <carlfenley-X-@-X-san.rr.com> wrote in message
news:OE**************@TK2MSFTNGP11.phx.gbl...
I am recursing through ALL folders and sub-folders below a certain level to
list all the files of a certain type in those folders. I use two
ArrayLists, alFiles and alFolders, to track matching files and my progress
in the recursion.

I use a while loop to manage the folder-subfolder drill-down. Each pass
through the while loop creates a new DirectoryInfo object named
currentFolder.

Is there are problem with that? Does this create multiple objects or does
it destroy the existing DirectoryInfo object and create a new one?

Here is the relevant source:
ArrayList alFiles = new ArrayList();
ArrayList alFolders = new ArrayList();
FileInfo[] files;

alFolders.Add(rootPath);
while(alFolders.Count > 0)
{
DirectoryInfo currentFolder = new DirectoryInfo(alFolders[0].ToString());
folders = currentFolder.GetDirectories();
foreach(DirectoryInfo subFolder in folders)
alFolders.Add(currentFolder.FullName + "\\" + subFolder.Name);

files = currentFolder.GetFiles();
foreach(FileInfo file in files)
{
if (file.Name.EndsWith("xml"))
alFiles.Add(file.Name);
}

alFolders.RemoveAt(0);
}
Nov 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.