在所有可能的文件夹中找到文件? [英] Find a file within all possible folders?

查看:190
本文介绍了在所有可能的文件夹中找到文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用c#在所有可能的目录中找到特定的文件(例如cheese.exe)?然后将路径存储到它找到的目录中?

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?

推荐答案

此代码片段检索所有逻辑驱动器的列表然后在驱动器上搜索与文件名Cheese.exe匹配的文件的所有文件夹。一旦循环完成,Listfiles包含

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<string>();
     //@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));
     }

这篇关于在所有可能的文件夹中找到文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆