Find a file within all possible folders?

前端 未结 2 420
再見小時候
再見小時候 2020-12-01 17:30

I was wondering how I could use c# to find a specific file (example cheese.exe) within all possible directories? And then store the path to the directory it found it in?

2条回答
  •  伪装坚强ぢ
    2020-12-01 18:05

    This code fragment retrieves a list of all logical drives on the machine and then searches all folders on the drive for files that match the filename "Cheese.exe". Once the loop has completed, the List "files" contains the

         var files = new List();
         //@Stan R. suggested an improvement to handle floppy drives...
         //foreach (DriveInfo d in DriveInfo.GetDrives())
         foreach (DriveInfo d in DriveInfo.GetDrives().Where(x => x.IsReady == true))
         {
            files.AddRange(Directory.GetFiles(d.RootDirectory.FullName, "Cheese.exe", SearchOption.AllDirectories));
         }
    

提交回复
热议问题