关于C# 操作 “Everything” 的使用,超好用

关于everything 的强大还真不是忽悠的,下面是自己的一些处理方法

1:新建一个控制台项目(这个任何项目都可以请随意开心就好)

2:下载  “Everything” 搜索引擎,下载 “ES” ES是一个命令行界面,用于从命令提示符搜索Everything;下载连接:https://www.voidtools.com/zh-cn/downloads/#cli

3:把 “Everything.exe” 和 “ES.exe” 放在项目根目录

以上是开发前的 “硬装” 缺一不可,一下以代码的形式为大家呈现 “软装”

1:新建一个控制台应用程序

2:添加一个 “CmdHelper” 工具类用于通过启动ES执行相关命令执行Everything搜索

    public class CmdHelper
    {
        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";

            using (Process p = new Process())
            {
                p.StartInfo.FileName = CmdPath;
                p.StartInfo.UseShellExecute = false;        //是否使用操作系统shell启动
                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();
            }
        }

    }

3:主程序传值启用,注意启动程序之前先启动“Everything” 

        public static void Main(string[] args)
        {
            string str = "";

            //比如搜索电脑里的"mp4",注意中间有空格
            string cmd = "es.exe *mp4";
            CmdHelper.RunCmd(cmd, out str);

            Console.WriteLine("{0}", str.ToString());
            Console.ReadKey();
        }

4:效果图

结尾:Everything是一个强大,轻便,灵活的搜索工具,对于特殊需求的开发还是极为有用的;出于好奇便研究了一下以供有兴趣的朋友参考一下,码农不易如果对你有用,打赏一下给个支持!

  

猜你喜欢

转载自www.cnblogs.com/yply/p/11363050.html