[英]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.