如何用c#在我们的系统中搜索媒体文件?

发布于 2024-08-26 19:48:19 字数 128 浏览 7 评论 0原文

我想通过 C# 搜索位于我系统中的媒体文件。意味着我想创建一个搜索引擎来扫描所有驱动器(这里又是一个小问题,如何通过 C# 代码获取我们系统上的驱动器?)并搜索 .mp3、.mp4、...等媒体文件。我如何通过 C# 桌面应用程序做到这一点?

I want to search the media files situated in my system by c#. means I want to create the search engine that will scan all drives (again small question here , how to get the drives on our system by c# code ? )and search the media files like .mp3,.mp4,...etc . How can i do that by c# desktop application?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

牵强ㄟ 2024-09-02 19:48:19

试试这个:

List<string> mediaExtensions = new List<string>{"mp3", "mp4"};
List<string> filesFound = new List<string>();

void DirSearch(string sDir) 
{
   foreach (string d in Directory.GetDirectories(sDir)) 
   {
    foreach (string f in Directory.GetFiles(d, "*.*")) 
    {
        if(mediaExtensions.Contains(Path.GetExtension(f).ToLower()))
           filesFound.Add(f);
    }
    DirSearch(d);
   }
}

try this:

List<string> mediaExtensions = new List<string>{"mp3", "mp4"};
List<string> filesFound = new List<string>();

void DirSearch(string sDir) 
{
   foreach (string d in Directory.GetDirectories(sDir)) 
   {
    foreach (string f in Directory.GetFiles(d, "*.*")) 
    {
        if(mediaExtensions.Contains(Path.GetExtension(f).ToLower()))
           filesFound.Add(f);
    }
    DirSearch(d);
   }
}
﹂绝世的画 2024-09-02 19:48:19

我建议使用 Windows 桌面搜索 API,而不是通过目录进行强力迭代搜索,这将快几个数量级。

通过 C# 进行 Windows 桌面搜索

Rather than a brute-force iterative search through directories, I recommend looking into using the Windows Desktop Search API, which will be orders of magnitude faster.

Windows Desktop Search via C#

や莫失莫忘 2024-09-02 19:48:19

获取驱动器列表:

string[] drives = Environment.GetLogicalDrives();

获取所有文件:

foreach(string drive in drives)
    string[] allFiles = Directory.GetFiles(drive, "*.*", SearchOption.AllDirectories);

使用递归获取所有文件:

List<string> allFiles = new List<string>();
private void RecursiveSearch(string dir)
{
    allFiles.Add(Directory.GetFiles(dir));
    foreach(string d in Directory.GetDirectories(dir))
    {
      RecursiveSearch(d);
    }  
}

使用 Manu 的答案进行过滤

To get your drive list:

string[] drives = Environment.GetLogicalDrives();

To get all your files:

foreach(string drive in drives)
    string[] allFiles = Directory.GetFiles(drive, "*.*", SearchOption.AllDirectories);

To get all your files using recursion:

List<string> allFiles = new List<string>();
private void RecursiveSearch(string dir)
{
    allFiles.Add(Directory.GetFiles(dir));
    foreach(string d in Directory.GetDirectories(dir))
    {
      RecursiveSearch(d);
    }  
}

Filter using Manu's answer

折戟 2024-09-02 19:48:19

试试这个

 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, "File Name", SearchOption.AllDirectories));
     }

try this

 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, "File Name", SearchOption.AllDirectories));
     }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文