Prady
Prady

Reputation: 11310

How do I search for files using C# on a windows OS

I have a csv file with file names and I need to search for those filenames in a particular directory. I can read thru a csv file and get all the filenames but would like to know how can I search for those files.

Any pointers would be would of great help

Upvotes: 2

Views: 133

Answers (3)

ExCodeCowboy
ExCodeCowboy

Reputation: 880

It's pretty easy, there are many different ways you can do this. I prefer the "exists" method if I am just trying to find out if the files are there.

string SearchDirectory = "C:\\SomeDirectory\\";
List<String> FilesToSearch = new List<string>();
//Populate FilesToSearch from your csv...
foreach (String CurrentFileToSearch in FilesToSearch)
{
    if (System.IO.File.Exists(SearchDirectory + TargetFileName))
    {
        //Do Something!
    }
}

Upvotes: 2

rerun
rerun

Reputation: 25505

System.IO.Directory.Getfiles will give you a list of files in a particular directory. If you need to so more intensive searching. You may want to use the windows indexing.

Upvotes: 3

crypted
crypted

Reputation: 10306

What about trying following code snippet.

Directory.EnumerateFiles(directory, fileName, SearchOption.AllDirectories);

Upvotes: 3

Related Questions