How to find list of file path in files and in sub

2019-09-10 04:09发布

问题:

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

回答1:

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 wmi-query