Problem creating FileInfo Array

A

Antonio

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 containing the a.jpg ,
a.jpg , ... , a.jpg
in a way that I could bind this to a datagrid to show some properties
of these files.
I tried the following but it is not ok

FileInfo[] fi ;
DirectoryInfo dirInfo = new DirectoryInfo("C:\HTML\Aziende");
DirectoryInfo[] di = dirInfo.GetDirectories();
int i = 0;
fi = new FileInfo[1000];
foreach (DirectoryInfo dri in di)
{
fi[i++] = dri.GetFiles("a.jpg")[0];
}

the error is System.IndexOutOfRangeException


could you help me to solve this problem and to crate the array of
FileInfo types ??

Thanks ...


Antonio
 
D

Dan Bass

rather than FileInfo[] fi, use ArrayList or another collection object. They
are dynamic and can grow the more objects you have, and can hold any object
type (FileInfo included)...

ArrayList fi = new ArrayList();
string fileName = "a.jpg";

foreach ( DirectoryInfo dri in di )
{
if ( dri.GetFiles(fileName).Count > 0 )
{
fi.Add ( dri.GetFiles(fileName)[0] );
}
}
 

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

Similar Threads


Top