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

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

问题描述

我想知道如何使用 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"匹配的文件.循环完成后,列表文件"包含

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天全站免登陆