如何在C#中通过WMI找到基于不同扩展名的C盘文件和子文件夹中的文件路径列表 [英] How to find list of file path in files and in sub folders of C Drive based on different extension through WMI in C#

查看:33
本文介绍了如何在C#中通过WMI找到基于不同扩展名的C盘文件和子文件夹中的文件路径列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从整个 C 驱动器中搜索扩展 mp3 和更多文件,并仅将其路径存储在特定列表字符串中,目前我只是在控制台上打印我也得到重复的结果.我只想跳过即使找到一个文件.如何搜索除 mp3 和 avi 等之外的所有类型的扩展文件

I need to search files of the extension mp3 and many more from the whole C Drive and store only its path in specific list string,currently I'm just printing on the Console I get repetitive results too .I just want to skip even if one file is found.How is it possible to search all type of extension file apart with mp3 and avi and many more

List<string> filesmediaFound = new List<string>();
                            string machinename = Environment.MachineName;
                            string wmifilepath = @"\\" + machinename + @"\root\CIMV2";
                            string fileSearchQuery = @"SELECT * FROM CIM_DataFile WHERE Drive='C:' AND Extension = 'mp3'";

                            // ObjectQuery querystring = new ObjectQuery(fileSearchQuery);
                            using (ManagementObjectSearcher search = new ManagementObjectSearcher(wmifilepath, fileSearchQuery)) 
                            {
                                foreach (ManagementObject result in search.Get())
                                {

                                    Console.WriteLine( result["Path"].ToString());
                                }
                            }

推荐答案

如果你只是想搜索文件那么你可以使用 System.IO 中的 Directory 类

If you just want to search for files then you can use Directory class in System.IO

Directory.GetFiles(@"C:\", "*.mp3". SearchOption.AllDirectories)

这篇关于如何在C#中通过WMI找到基于不同扩展名的C盘文件和子文件夹中的文件路径列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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