0

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());
                                }
                            }
TechBrkTru
  • 346
  • 1
  • 25

1 Answers1

-1

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

Directory.GetFiles(@"C:\", "*.mp3". SearchOption.AllDirectories)
Sandeep
  • 399
  • 5
  • 15
  • Well it doesnt allow me to search files that have access denied like Users Folder.. I want to search them through WMI @Sandeep – TechBrkTru Jul 24 '15 at 07:01
  • I doubt if you will be able to access those folders with WMI. You can handle such scenario in your code. Any particular reason to use WMI api – Sandeep Jul 24 '15 at 07:06
  • Was facing difficulty with IO check out the link[http://stackoverflow.com/questions/31581772/how-to-get-file-path-from-c-program-files-folder-in-c-sharp-while-searching-spe] so swtiched to find out with WMI @Sandeep – TechBrkTru Jul 24 '15 at 07:07
  • I found a possible solution to your problem in the comments: http://stackoverflow.com/questions/163162/can-you-call-directory-getfiles-with-multiple-filters. In case that doesn't work you may want to run the recurse through the directory yourself. – Sandeep Jul 24 '15 at 07:15