当前位置: 首页 > news >正文

c# everthing.exe 通信

1 获取everthing进程  调用 Everything 搜索创建SearchWithEverything函数

using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.IO;
using System.Management;
using System.Text;class EverythingHelper
{// 方法 1:从进程获取路径public static string GetEverythingPathFromRunningProcess(){Process[] processes = Process.GetProcessesByName("Everything");if (processes.Length == 0){return null; // Everything 未运行}try{return processes[0].MainModule.FileName;}catch{return null; // 权限不足}}// 方法 2:从 WMI 获取路径(更可靠)public static string GetEverythingPathFromWMI(){string query = "SELECT ExecutablePath FROM Win32_Process WHERE Name = 'Everything.exe'";try{using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))using (ManagementObjectCollection results = searcher.Get()){if (results.Count == 0) return null;foreach (ManagementObject process in results){try{object pathObj = process["ExecutablePath"];if (pathObj != null){string exePath = pathObj.ToString();if (!string.IsNullOrWhiteSpace(exePath) && File.Exists(exePath)){return exePath;}}}catch (ManagementException ex){// 记录错误或忽略(权限不足等)Debug.WriteLine($"WMI Access Error: {ex.Message}");}}}}catch (Exception ex){Debug.WriteLine($"WMI Query Failed: {ex.Message}");}return null;}// 方法 3:从注册表获取public static string GetEverythingPathFromRegistry(){using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Everything")){if (key != null){object installLocationObj = key.GetValue("InstallLocation");string installLocation = installLocationObj?.ToString(); // 安全转换if (!string.IsNullOrEmpty(installLocation)){string exePath = Path.Combine(installLocation, "Everything.exe");if (File.Exists(exePath)) return exePath;}}}return null;}// 获取 Everything 选中的文件和目录列表// 版本检测:需要 Everything 1.4.1+public static bool CheckEverythingVersion(){try{string exePath = FindEverythingExe();var versionInfo = FileVersionInfo.GetVersionInfo(exePath);return versionInfo.FileMajorPart > 1 ||(versionInfo.FileMajorPart == 1 && versionInfo.FileMinorPart >= 4);}catch{return false;}}// 增强版获取选中项(带状态反馈)public static (List<string> items, string message) GetSelectedItemsWithStatus(){try{// 检查 Everything 是否运行if (Process.GetProcessesByName("Everything").Length == 0)return (new List<string>(), "Everything 未运行");if (!CheckEverythingVersion())return (new List<string>(), "需要 Everything 1.4.1 或更高版本");var items = new List<string>();string exePath = FindEverythingExe();using (Process process = new Process()){process.StartInfo = new ProcessStartInfo{FileName = exePath,Arguments = "-export-selected-items-to-stdout",UseShellExecute = false,RedirectStandardOutput = true,CreateNoWindow = true,StandardOutputEncoding = Encoding.UTF8};process.Start();// 异步读取输出while (!process.StandardOutput.EndOfStream){string line = process.StandardOutput.ReadLine()?.Trim();if (!string.IsNullOrEmpty(line) && (File.Exists(line) || Directory.Exists(line))){items.Add(line);}}if (!process.WaitForExit(3000)) // 最多等待3秒{process.Kill();return (items, $"获取超时,已找到 {items.Count} 个有效项");}}return items.Count > 0? (items, $"成功获取 {items.Count} 个选中项"): (items, "Everything 中没有选中的有效文件/目录");}catch (Exception ex){return (new List<string>(), $"获取失败: {ex.Message}");}}// 显示选中项在 RichTextBox(带格式)public static void DisplaySelectedItems(RichTextBox box){var (items, status) = GetSelectedItemsWithStatus();box.Clear();box.SelectionColor = Color.Blue;box.AppendText(status + "\n\n");for (int i = 0; i < items.Count; i++){// 序号box.SelectionColor = Color.Green;box.AppendText($"{i + 1}. ");// 路径类型bool isDir = Directory.Exists(items[i]);box.SelectionColor = isDir ? Color.Orange : Color.Black;box.AppendText(isDir ? "[目录] " : "[文件] ");// 路径box.SelectionColor = Color.Black;box.AppendText(items[i] + "\n");}}// 综合查找public static string FindEverythingExe(){// 1. 尝试从运行进程获取string exePath = GetEverythingPathFromWMI() ?? GetEverythingPathFromRunningProcess();if (exePath != null) return exePath;// 2. 尝试从注册表获取exePath = GetEverythingPathFromRegistry();if (exePath != null) return exePath;// 3. 尝试默认路径string[] commonPaths ={@"C:\Program Files\Everything\Everything.exe",@"C:\Program Files (x86)\Everything\Everything.exe",Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Everything", "Everything.exe")};foreach (string path in commonPaths){if (File.Exists(path)) return path;}// 4. 让用户手动选择OpenFileDialog openFileDialog = new OpenFileDialog{Title = "请选择 Everything.exe",Filter = "Everything.exe|Everything.exe|所有文件 (*.*)|*.*",InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)};if (openFileDialog.ShowDialog() == DialogResult.OK)  // 不是 true,而是 DialogResult.OK{return openFileDialog.FileName;}throw new FileNotFoundException("未找到 Everything.exe。");}// 调用 Everything 搜索public static void SearchWithEverything(string query){string everythingExe = FindEverythingExe();Process.Start(everythingExe, $"-s \"{query}\"");}
}

http://www.dtcms.com/a/301656.html

相关文章:

  • Level MC-9“恶地”
  • 1.qt历史版本安装与多版本开发(解决被拦截问题)
  • 青少年编程能力等级测评试卷及答案 Python编程(三级)
  • 《频率之光:共生之恋》
  • C9300L Stacking堆叠
  • 人形机器人指南(十)决策
  • 快速了解线性回归算法
  • Item16:成对使用new和delete时要采取相同形式
  • Sklearn 机器学习 数值指标 混淆矩阵confusion matrix
  • Java知识体系
  • 初识 docker [上]
  • 【高等数学】第六章 定积分的应用——第三节 定积分在物理学上的应用
  • GO语言 go get 下载 下来的包存放在哪里
  • 线程安全问题的发现与解决
  • 2025 DevOps开源工具全景指南:构建面向未来的智能交付体系
  • 嵌入式软件面试八股文
  • 面试150 数字范围按位与
  • PLLIP核
  • 测试老鸟整理,物流项目系统测试+测试点分析(一)
  • 【笔记】Gibbs自由能全微分公式推导
  • AJAX 原理_第一节_XHR 对象
  • 免安装MySQL启动全解:从解压到远程访问的保姆级教程
  • U盘中毒,文件被隐藏的解决方法
  • Redis6.0+安装教程(Linux)
  • Map系列
  • docker搭建部署 onlyoffice 实现前端集成在线解析文档解决方案
  • 车载诊断架构 ---面向售后的DTC应该怎么样填写?
  • net8.0一键创建支持(Kafka)
  • 基于Prometheus+Grafana的分布式爬虫监控体系:构建企业级可观测性平台
  • 【旧文】Adobe Express使用教程