我需要搜索扩展程序mp3的文件以及更多来自整个C驱动器的文件并仅将其路径存储在特定列表字符串中,目前我只是在控制台上打印我也得到了重复的结果。我只是想要即使找到一个文件也要跳过。如何用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());
}
}
答案 0 :(得分:-1)
如果您只想搜索文件,则可以在System.IO中使用Directory类
Directory.GetFiles(@"C:\", "*.mp3". SearchOption.AllDirectories)