简体   繁体   中英

How to find list of file path in files and in sub folders of C Drive based on different extension through WMI in C#

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类

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM