简体   繁体   English

如何通过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#

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 我需要从整个C驱动器中搜索扩展名为mp3的文件以及更多文件,并将其路径仅存储在特定的列表字符串中。目前,我只是在控制台上打印,我也得到了重复的结果。即使一个找到文件。如何用mp3和avi等搜索所有类型的扩展文件

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何列出硬盘驱动器上的所有文件和文件夹? - c# - How to list all files and folders on a hard drive? 如何将文件从文件夹和子文件夹传输到 C# 中由文件名模式创建的特定文件夹(File2specific Folders) - How to transfer files form folders and sub-folders to specific folders created by file name pattern in C# (File2specific Folders) 如何获取特定路径中的WMI类列表(使用C#)? - How to get a list of WMI classes in certain path (using C#)? Rackspace CloudFiles C#API:如何在根“文件夹”中列出文件,以及如何在“文件夹”中列出(子)文件夹? - Rackspace CloudFiles C# API: How to list files on root 'folder', and how to list (sub)folders within a 'folder'? 通过 WMI 检索硬盘信息 .NET C# - Retrieve Hard drive info through WMI .NET C# 如何获取azure文件共享存储c#中子文件夹内的所有文件? - How to get all files which is inside sub-folders in azure file share storage c#? 根据文件夹 C# Winforms 的内容检查和列出子文件夹 - Check and list sub-folders based on contents of folders C# Winforms 通过 c# 读取 C 驱动器中的所有文件和文件夹 - Read all files and folders in C drive via c# 如何在不知道文件路径的情况下使用 C# 在 Outlook 中找到实现文件夹 - How to find Achieve folders in Outlook without knowing the file path using C# 如何在 C# 中找到文件的扩展名? - How to find the extension of a file in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM