C#调用everything工具进行文件搜索功能

您所在的位置:网站首页 bi前缀的英语单词有哪些 C#调用everything工具进行文件搜索功能

C#调用everything工具进行文件搜索功能

2024-06-18 01:03:48| 来源: 网络整理| 查看: 265

前言:

根据需求,现在需要任意输入输入批次号,然后软件到共享文件夹里面搜索这个CSV文件拷贝到我们的分拣表格里面,以及搜索这个批次号下面的文件夹里面的 可以运行 的加工文件拷贝到Z盘里面(Z盘存在网络映射文件夹)

难点:

1.电脑自带的搜索工具遍历搜索比较困难,文件多会卡住程序;

2.选取指定路径时,可能存在找不到路径(访问权限不足,导致无法访问共享文件夹;防火墙或安全软件阻止了网络连接等等);如果您的程序以管理员身份运行,则可能会受到UAC(用户帐户控制)的影响,导致无法访问网络映射驱动器

3.需求需要根据批次号,再去路径下,找到该批次号名称所在的文件夹,然后找到所有的加工文件,一键复制到指定目录

准备工作: 配置要求系统windows10以上软件Visual Studio 2022、everything接口ES

开始

新建一个winform窗体项目,找到该项目的文件目录地址,将Everything” 搜索引擎、ES下载放在debug文件夹的bin目录下

everything下载(找最新版的就好,先下载好,自己试试能不能搜索自己想要的文件);

ES接口下载:ES 是让用户在命令提示符中使用 Everything 搜索的命令行接口。

 

需求

Everything 必须已安装并运行中。

窗体设计

大概设计成这种窗体,样式自定义就好

 

我的程序名称为:

控件控件名称控件控件名称label6打孔文件后缀label4分拣文件后缀label5批次号:btnCopy 复制txtChType.mprtxtType.csvLBlChildlabel1 btnSz地址设置lBltxtboxprog

附上一个小程序:搜索所有窗体,将窗体的名称输出:

            foreach (Control control in this.Controls)             {                 string name = control.Name;                 string text = control.Text;                                  Console.WriteLine($"Name: {name}, Text: {text}");             }

设计主要代码:

 

 代码保存与读取的路径存放在App.config,方便下次读取与保存

配置文件准备:App.config:

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

using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace everythingTest { internal class CmdHelper { private static string CmdPath = @"C:\Windows\System32\cmd.exe"; public static void RunCmd(string cmd, out string output) { cmd = cmd.Trim().TrimEnd('&') + "&exit"; using (Process p = new Process()) { p.StartInfo.FileName = CmdPath; 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(); } } } }

1.模糊搜索功能

//模糊搜索 private void search_Changed(object sender, EventArgs e) { //要求:读取csv // 读取配置文件中的路径 string folderpath = ConfigurationManager.AppSettings["FolderPath"]; //设置关键字 string name = this.txtbox.Text.Trim(); //设置搜索文件扩展名 string type = txtType.Text.Trim(); string cmd = $@"es.exe *{name}*{type} -path {folderpath}"; CmdHelper.RunCmd(cmd, out string str); lBl.DataSource = GetFileList(str); lBl.Refresh(); }

2.文件选中操作事件

//文件选中 操作 private void thevalueChanged(object sender, EventArgs e) { LBlChild.DataSource = null; LBlChild.Items.Clear(); label3.Text = ""; if (lBl.SelectedItem == null || string.IsNullOrEmpty(lBl.SelectedItem.ToString())) { return; } // 读取文件名 比如B230223030 string fileName = Path.GetFileNameWithoutExtension(lBl.SelectedItem.ToString()); // 读取配置文件中要搜索的路径 比如C:\\Users\\DELL\\Desktop string folderpath2 = ConfigurationManager.AppSettings["FolderPath2"]; //直接获取文件夹名称的路径 例如搜索demo C:\\Users\\DELL\\Desktop\\demo string cmd = $"es.exe -s folder:ww:{fileName} {folderpath2}"; CmdHelper.RunCmd(cmd, out string str); var list = GetFileList(str); //获取文件夹全路径 string tempath = list[0]; string path = $"{tempath}\\MPR56"; //需要搜索的类型 string type = txtChType.Text.Trim(); string cmd2 = $@"es.exe *{type} -path {path}"; CmdHelper.RunCmd(cmd2, out string str2); var list2 = GetFileList(str2); LBlChild.DataSource = list2; LBlChild.Refresh(); label3.Visible = true; label3.Text = $"在MPR56文件夹共搜索出{list2.Count - 1}个文件"; // 控件全选 for (int i = 0; i < LBlChild.Items.Count; i++) { LBlChild.SetSelected(i, true); } }

3.一键复制操作

//复制 private void SelectedItems(object sender, EventArgs e) { List selectedFiles = new List(); foreach (string file in lBl.SelectedItems) { selectedFiles.Add(file); } //复制文件到指定文件夹 string targetFolder = ConfigurationManager.AppSettings["Path"]; foreach (string file in selectedFiles) { string fileName = Path.GetFileName(file); string targetPath = Path.Combine(targetFolder, fileName); File.Copy(file, targetPath, true); //覆盖已存在的文件 } //搜索文件夹下的MPR56的加工文件 List selectedFiles2 = new List(); foreach (string file in LBlChild.SelectedItems) { selectedFiles2.Add(file); } //复制mpr文件到指定文件夹 string Path2 = ConfigurationManager.AppSettings["Path2"]; foreach (string file in selectedFiles2) { string fileName = Path.GetFileName(file); string targetPath = Path.Combine(Path2, fileName); if (!string.IsNullOrEmpty(file)) { File.Copy(file, targetPath, true); //覆盖已存在的文件 } } for (int i = 1; i


【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭