473,385 Members | 1,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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.

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
1 2595
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Senthil | last post by:
Con is the file name for a reserved device name(i think it is for console). So you cannot create a file with name 'con'. choose some other name senthil >-----Original Message----- >Hi...
9
by: Sheldon Cohen | last post by:
Hello, I am using c# and running a site that is on a shared host. The code in question is supposed to create a new directory that is coming out of a text box. It works fine on my computer, but...
1
by: Antonio | last post by:
Good morning, I've the following file system : C: -> HTML -> Aziende -> Azienda_1 -> a.jpg -> Azienda_2 -> a.jpg ... -> Azienda_N -> a.jpg my desire is to create an array of fileinfo...
8
by: Lyners | last post by:
I have code that retrieves all of the file names within a directory. After retriving them, I display the information in a datagrid. What I would like to do is add an extra output column on the...
13
by: Tom Scales | last post by:
OK, I admit, I am not a VB.NET expert and am still learning, but have run into an annoying problem. I'm writing a program that has to search the file system and therefore processes large numbers...
3
by: Erik Foreman | last post by:
I am using a folderbrowserdialog object to prowse to a folder then once in that folder I am going to have my program rename all of the files in that folder. I know the rename funchtion but I am not...
2
by: rn5a | last post by:
Suppose I have the following code: <script runat="server"> Sub Page_Load(.....) Dim dInfo As DirectoryInfo Dim fsi As FileSystemInfo dInfo = New DirectoryInfo(Server.MapPath("Folder1")) ...
6
by: Charlie Bear | last post by:
i'm really stuck with this one can anyone help! i have a website that uses c#. it creates a series of directories and files from an xml source. when the xml changes, the directory that the...
5
by: Tom P. | last post by:
I am having the following problem: I create a FileSystemWatcher and wait for events. When the event does happen I refresh a FileSystemInfo list and set properties accordingly (IsFile, IsDir,...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.