> 技术文档 > c# everthing.exe 通信

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 items, string message) GetSelectedItemsWithStatus() { try { // 检查 Everything 是否运行 if (Process.GetProcessesByName(\"Everything\").Length == 0) return (new List(), \"Everything 未运行\"); if (!CheckEverythingVersion()) return (new List(), \"需要 Everything 1.4.1 或更高版本\"); var items = new List(); 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(), $\"获取失败: {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}\\\"\"); }}