Add an Array to an Array (Or a better way to do this)

C

Craig Lister

I am trying to get a list of all files in a directory (Recursivly).

The way I am trying to do it, it looks like I need to add an array of items
to an array of items. The way I am ACTUALLY doing it doesn't work... The
code where I *attempt* to add the two arrays together is wrong... it's just
code to show what I am trying to do..

Also, is this a good way to do what I am attempting to do ?

The 'ClassFiles' class isn't shown in this code.

private ArrayList GetFiles( string DirectoryName )

{

ArrayList myArray = new ArrayList(); // Return type. Will hold list
of all Objects



DirectoryInfo di = new DirectoryInfo(DirectoryName);

DirectoryInfo[] dirs = di.GetDirectories();

foreach (DirectoryInfo diNext in dirs)

{

myArray = myArray + GetFiles(diNext.FullName); // NO!

}

FileInfo[] rgFiles = di.GetFiles("*.*");

foreach (FileInfo fi in rgFiles)

{

ClassFiles FileObject = new ClassFiles();

FileObject.FileName = fi.FullName;

FileObject.FileSize = fi.Length;

FileObject.FileDateTime = fi.LastWriteTime;

myArray.Add(FileObject);

}

return myArray;

}
 
C

Craig Lister

Aha!!

myArray.AddRange(GetFiles(diNext.FullName));

seems to work!

Is this an OK way to do this though?
 
P

Paul Henderson

myArray.AddRange(GetFiles(diNext.FullName));
seems to work!
Is this an OK way to do this though?

Looks fine to me with AddRange; there's no way to neaten it up
particularly, so I guess it's an "OK way" [I certainly can't think of a
neater solution].
 
J

Jon Skeet [C# MVP]

Craig Lister said:
Aha!!

myArray.AddRange(GetFiles(diNext.FullName));

seems to work!

Is this an OK way to do this though?

Well, it's okay - but an alternative is to pass in the ArrayList you
want to fill. You could have two overloads - one which doesn't take an
ArrayList, and one which does. The first can create a new one, call the
second, and then return the list it's created. The second adds to
whatever list you pass it.

Just for future reference - it's worth distinguishing between an array
(which is of fixed size) and an ArrayList (which isn't). Fortunately
your sample code showed what you meant :)
 
C

Craig Lister

Great, thanks, but..
"Well, it's okay - but an alternative is to pass in the ArrayList you want
to fill." - do you feel here is any benefit in doing this?
 
J

Jon Skeet [C# MVP]

Craig Lister said:
Great, thanks, but..
"Well, it's okay - but an alternative is to pass in the ArrayList you want
to fill." - do you feel here is any benefit in doing this?

Well, it means less copying around - you end up only creating one list
and adding to it, rather than perpetually creating a new list, then
copying the contents of that to one main list.
 
C

Craig Lister

Ah!
In the process of changing it now then! :)

Jon Skeet said:
Well, it means less copying around - you end up only creating one list
and adding to it, rather than perpetually creating a new list, then
copying the contents of that to one main list.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top