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
2009-10-18 12:02:04
+2
A:
You can do something like this:
string[] files = Directory.GetFiles(@"c:\myfolder", "*.txt", SearchOption.AllDirectories)
Jan
2009-10-18 12:03:24
+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
2009-10-18 12:05:05
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
2010-05-02 17:47:33
Nice comments dude
alexn
2010-05-02 18:17:36