I recently wrote a little application which uploads specific files on a specific FTP. I came across the System.IO.Directory.GetFiles() Method and noticed that it’s natively only possible to add one kind of pattern. Which means:
string[] files = Directory.GetFiles(path, "*");
Works without problems. But what if you like to insert multiply extensions like:
string[] files = Directory.GetFiles(path, "*.jpg *.png *.gif");
This won’t work that way! The function only accepts one filter. And that’s how to add more than one:
string strFilter = "*.jpg;*.png;*.gif";
string[] m_arExt = strFilter.Split(';');
foreach(string filter in m_arExt)
{
string[] strFiles = Directory.GetFiles(folder, filter);
}
ak
10 comments ↓
Hey; great help.
I assume we figure out for our own that we should add up all the strFiles to a seperate array, otherwise in the end strFiles will only have the files belonging to the final filter (assuming strFiles be declared before the for loop)? correct? or am I foolishly missing something?
Thank you for the tip!
This is what I ended up using:
public static string [] GetVideoList (string path)
{
string filter = "*.mpg;*.gif" ;
string [] filterSplit = filter.Split (';') ;
int filterCount = filterSplit.Length ;
string [] fileList = null ;
string [][] jaggedFileList = new string[filterCount][] ;
int fileCount = 0 ;
int jaggedCount ;
if (Directory.Exists (path))
{
for (int i = 0; i
I actually streamlined your code a bit. Here take a look at this:
Thanks Tim!
Good HowTo!
Does anyone know of an example of binding it to a datagrid?
this works is good but there something missed.
When you apply two filters like (*.jpg; *.*), you can get the same files many times in the arrayList (in this case all jpg files will appears 2 times: one for *.jpg and one for *.*).
Then we need to add a function like DISTINCT() to make the list just needed.
thx.
This example is exactly what i’m looking for however very new to .NET, would anyone have the full working code – with it doing the databind bit
basically the complete start to finish code – i can compare and see where i’ve gone wrong
Thanks
This is not updating the array list !! Any ideas??
[code] public string ExtensionFilter {
get { return string.Join(“;”, _extentionFilters); }
set { _extentionFilters = value.Split(‘;’); }
}
private string[] _extentionFilters;[/code]
string[] files = Directory.GetFiles(path, “*”+urSearchstring+”*”);
Leave a Comment