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

[亡魂溺海] 提交于 2019-12-11 19:26:35

问题


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)


来源:https://stackoverflow.com/questions/31604027/how-to-find-list-of-file-path-in-files-and-in-sub-folders-of-c-drive-based-on-di

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!