tags:

views:

298

answers:

4

How do I use wildcards in C# to list down files contained in a selected folder?

+10  A: 

Directory.GetFiles is your friend here:

Directory.GetFiles(@"C:\Users\Me\Documents", "*.docx");

or, recursively:

Directory.GetFiles(
    @"C:\Users\Me\Documents",
    "*.docx",
    SearchOption.AllDirectories);
Joey
+2  A: 

You can do something like this:

string[] files = Directory.GetFiles(@"c:\myfolder", "*.txt", SearchOption.AllDirectories)
Jan
+5  A: 
using System.IO;

DirectoryInfo folder = new DirectoryInfo(@"c:\blah");
if (folder.Exists) // else: Invalid folder!
{
  FileInfo[] files = folder.GetFiles("*.xml");

  foreach (FileInfo file in files)
  {
    DoSmething(file.FullName);
  }
}
Serge - appTranslator
A: 
private void ListFiles()
{

    //get the user calling this page 
    Gaf.Bl.User userObj = base.User;
    //get he debug directory of this user
    string strDebugDir = userObj.UserSettings.DebugDir;
    //construct the Directory Info directory 
    DirectoryInfo di = new DirectoryInfo(strDebugDir);
    if (di.Exists == true)
    {

        //get the array of files for this 
        FileInfo[] rgFiles = di.GetFiles("*.html");
        //create the list ... .it is easier to sort ... 
        List<FileInfo> listFileInfo = new List<FileInfo>(rgFiles);
        //inline sort descending by file's full path 
        listFileInfo.Sort((x, y) => string.Compare(y.FullName, x.FullName));
        //now print the result 
        foreach (FileInfo fi in listFileInfo)
        {
            Response.Write("<br><a href=" + fi.Name + ">" + fi.Name + "</a>");
        } //eof foreach
    } //eof if dir exists

} //eof method 
YordanGeorgiev
Nice comments dude
alexn