49

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

1
  • Could you explain what you want to do a little better please? Listing the files whose filenames match your string or some other thing? Commented Oct 18, 2009 at 12:02

3 Answers 3

109

Directory.GetFiles is your friend here:

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

or, recursively:

Directory.GetFiles( @"C:\Users\Me\Documents", "*.docx", SearchOption.AllDirectories); 
Sign up to request clarification or add additional context in comments.

Comments

24

Use DirectoryInfo.GetFiles

using System.IO; DirectoryInfo folder = new DirectoryInfo(@"C:\foo\bar"); if (folder.Exists) // else: Invalid folder! { FileInfo[] files = folder.GetFiles("*.xml"); foreach (FileInfo file in files) { DoSomething(file.FullName); } } 

Comments

7

You can do something like this:

string[] files = Directory.GetFiles(@"c:\myfolder", "*.txt", SearchOption.AllDirectories) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.