Re: Creating same new DirectoryInfo in loop?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Peter Rilling (peter_at_nospam.rilling.net)
Date: 08/07/04


Date: Fri, 6 Aug 2004 23:27:15 -0700

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:OErdo6CfEHA.1604@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);
}



Relevant Pages

  • Creating same new DirectoryInfo in loop?
    ... 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. ... Does this create multiple objects or does it destroy the existing DirectoryInfo object and create a new one? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Panicware pop-up stopper--A big Nuisance!
    ... Sunny wrote: ... > I also did a search in files and folders and found it on ... SpybotSD - Search & Destroy by PepiMK Software: ...
    (microsoft.public.windowsxp.security_admin)
  • Re: cannot uninstall missing folders
    ... that would destroy WMP's back-up folders, breaking rollback/uninstall. ... See http://zachd.com/pss/pss.html for some helpful WMP info. ...
    (microsoft.public.windowsmedia.player)
  • Copied photos to CD
    ... While trying to back up my hard drive before service trip, I loaded folders ... command was I performed a spybot search and destroy. ... restore program that can look at a jpg, bmp file and reconfigure it back to ...
    (microsoft.public.windowsxp.photos)
  • strange folders
    ... >I have folders on my C drive with names like ... >What are these and is it safe to delete them? ... will destroy you're motherboard. ...
    (microsoft.public.windowsxp.general)