471,309 Members | 1,080 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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

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;

}
Jan 8 '06 #1
6 1001
Aha!!

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

seems to work!

Is this an OK way to do this though?

"Craig Lister" <My*********@thelisters.co.uk> wrote in message
news:43***********************@news.zen.co.uk...
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;

}

Jan 8 '06 #2
Craig Lister <My*********@thelisters.co.uk> wrote:
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 :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 8 '06 #3
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?

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...
Craig Lister <My*********@thelisters.co.uk> wrote:
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 :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jan 8 '06 #4
Craig Lister <My*********@thelisters.co.uk> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 8 '06 #5
Ah!
In the process of changing it now then! :)

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Craig Lister <My*********@thelisters.co.uk> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jan 8 '06 #6
> 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].

Jan 9 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Nicolas Fleury | last post: by
2 posts views Thread by Gus Tabares | last post: by
reply views Thread by Chris Lambacher | last post: by
2 posts views Thread by John Machin | last post: by
reply views Thread by JM | last post: by
5 posts views Thread by bearophileHUGS | last post: by
1 post views Thread by Fabio | last post: by
reply views Thread by rosydwin | last post: by

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.