how to search the file using the C#.net windows application ?

Recommended Answers

All 2 Replies

>how to search the file using the C#.net windows application ?

Using System.IO.Directory.GetFiles() method.

The GetDirectories and GetFiles methods of the Directory object are all that you need to recursively search for files that match the search string. The following method is used to perform the recursion:

void DirSearch(string sDir) 
{
	try	
	{
	   foreach (string d in Directory.GetDirectories(sDir)) 
	   {
		foreach (string f in Directory.GetFiles(d, txtFile.Text)) 
		{
		   lstFilesFound.Items.Add(f);
		}
		DirSearch(d);
	   }
	}
	catch (System.Exception excpt) 
	{
		Console.WriteLine(excpt.Message);
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.