2013-07-23 20 views
-2

我想獲得給定文件名的物理路徑。對於是否可以在.net中獲得給定文件名的物理路徑?

EX:我有我的項目在驅動器E:\中,並且需要搜索駐留在C驅動器中的「x.txt」文件的物理路徑。

+0

是/否的問題,答案是可能的。 – Praveen

+0

重複[c#在所有可能的文件夾中找到文件?](http://stackoverflow.com/questions/1225294/c-sharp-find-a-file-within-all-possible-folders)。你有什麼嘗試? – CodeCaster

回答

1

是的。

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

如果您需要該文件的內容的工作,你可以使用FileInfo開始:

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

將其組合在一起:

var matchingFiles = Directory.EnumerateFiles(@"C:\") 
         .Where(filePath => filePath.EndsWith("x.txt")) 
         .Select(filePath => new FileInfo(filePath).FullName); 
相關問題