-2

I want to get the physical path of given file name. For

EX: i have my project in drive E:\ and need to search the physical path of "x.txt" file which is resides in C drive.

  • yes/no question, the answer is Possible. – Praveen Jul 23 '13 at 07:29
  • Duplicate of [c# Find a file within all possible folders?](http://stackoverflow.com/questions/1225294/c-sharp-find-a-file-within-all-possible-folders). What have you tried? – CodeCaster Jul 23 '13 at 08:19

1 Answers1

1

Yes.

var matchingFilePaths = Directory.EnumerateFiles(@"C:\")
                          .Where(filePath => filePath.EndsWith("x.txt"));

If you need to work with the file contents, you can start with using FileInfo:

var fileInfo = new FileInfo("path\to\file.ext");
var fullPath = fileInfo.FullName;

Putting it together:

var matchingFiles = Directory.EnumerateFiles(@"C:\")
                      .Where(filePath => filePath.EndsWith("x.txt"))
                      .Select(filePath => new FileInfo(filePath).FullName);
Sameer Singh
  • 1,358
  • 1
  • 19
  • 47