Everything
everything的下载 https://www.voidtools.com/zh-cn/
在下载页面往下拉,我们还需要 Everything的命令行接口工具 ES.exe
ES.exe的使用
在官网中也有介绍这个工具如何使用以及一些案例,https://www.voidtools.com/zh-cn/support/everything/command_line_interface/
举个例子 查找F盘中后缀为zip的压缩包
打开命令行界面,进入es.exe的目录下,(我这里的es.exe放在D:\MyEverything目录),输入查找命令: *es.exe F: .zip
查找结果如下:
注意:这个es.exe工具,需要先打开Everything软件,才可以使用!
C#调用Everything查找文件
大体思路:C#调用命令行,控制es.exe的运行和查询内容,最后再读取命令行运行结果即可。
EverythingHelper类
代码如下,需要 使用 using System.Diagnostics; 命名空间
public class EverythingHelper{private static string CmdPath = @"C:\Windows\System32\cmd.exe";public static void RunCmd(string cmd, out string output){//说明:不管命令是否成功均执行exit命令,//否则当调用ReadToEnd()方法时,会处于假死状态cmd = cmd.Trim().TrimEnd('&') + "&exit";output = string.Empty;using (Process p = new Process()){p.StartInfo.FileName = CmdPath;//es.exe的目录下运行命令行p.StartInfo.WorkingDirectory = @"D:\MyEverything";//是否使用操作系统flash启动p.StartInfo.UseShellExecute = false;//接受来自调用程序的输入信息p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输出信息p.StartInfo.RedirectStandardOutput = true;//重定向标准错误输出p.StartInfo.RedirectStandardError = true;//不显示程序窗口p.StartInfo.CreateNoWindow = true;//启动程序p.Start();//向cmd窗口写入命令p.StandardInput.WriteLine(cmd);p.StandardInput.AutoFlush = true;//获取cmd窗口的输出信息output = p.StandardOutput.ReadToEnd();//等待程序执行完退出进程p.WaitForExit();p.Close();}}}
UI界面
效果图如下,在F盘中查找后缀为zip且文件名字带有VS的文件。搜索功能具体实现的代码:
/// <summary>/// 搜索/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void BtnSearch_Click(object sender, EventArgs e){string cmdStr = string.Empty;if(tbDir.Text.Length > 0){//路径cmdStr += tbDir.Text + " ";}if(tbKeyWord.Text.Length > 0){//关键词cmdStr += tbKeyWord.Text + " ";}if(tbSuffix.Text.Length > 0){//后缀cmdStr += "*." + tbSuffix.Text;}if(cmdStr.Length < 1){return;}string resultStr;EverythingHelper.RunCmd("es.exe " + cmdStr, out resultStr);if(resultStr.Length > 0){tbOutput.Text = "";//记录到初步的完整路径List<string> AllPath = new List<string>();//分割得到的字符串SpitPath(ref resultStr, ref AllPath);foreach(string path in AllPath){tbOutput.Text += path + System.Environment.NewLine;}}}/// <summary>/// 处理Evething得到的数据/// </summary>/// <param name="reslutPath">cmd命令得到的字符串</param>/// <param name="AllPath"></param>private static void SpitPath(ref string reslutPath, ref List<string> AllPath){string tempStr = string.Empty;//分割命令行的字符串string[] tempstr = reslutPath.Split(new String[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);for (int i = 0; i < tempstr.Length; i++){if (!File.Exists(tempstr[i])){//不是文件路径则继续往下判断continue;}else{if (tempStr.Equals(tempstr[i])){//路径重复continue;}else{//记录文件的路径tempStr = tempstr[i];AllPath.Add(tempstr[i]);}}}}
小结
Everything小巧便捷且功能搜索功能文件功能强大,还有很多不同条件的查找功能。感谢开发者给我们免费使用!最后,附上该Demo的完整工程。https://download.csdn.net/download/weixin_40314351/86399754?spm=1001.2014.3001.5503