CODEDIGEST
Home » CodeDigest
Search
 

Technologies
 

How to search a folder or file under a directory using wildcard in C#?
Submitted By Bala Murugan
On 12/2/2010 8:28:12 AM
Tags: C#,CodeDigest  

How to search a folder or file under a directory using wildcard in C#?

 

The Directory class packed with System.IO namespace has a method called GetDirectories() which can be used to search a folder.


This little code snippet will help us to search a folder using wildcard characters in GetDirectories() method.


To list all folders that start with lazyload word,


string[] dirs = Directory.GetDirectories(@"E:\bala\Technicals\Samples", "lazyload*", SearchOption.AllDirectories);
foreach (string dir in dirs)
        {
Response.Write(dir + "<br>");
        }

 

To list all files that start with the word User,
string[] files = Directory.GetFiles(@"E:\bala\Technicals\Samples", "User*", SearchOption.AllDirectories);
Response.Write("<b>List of file</b><br>");
foreach (string file in files)
        {
Response.Write(file + "<br>");
        }

 

Include System.IO namespace for the above code snippets to work.


 Happy Coding!!

Do you have a working code that can be used by anyone? Submit it here. It may help someone in the community!!

Recent Codes
  • View All Codes..