using System.Diagnostics; using System.IO; using Microsoft.Win32; namespace AxCopilot.Services; /// /// Windows 탐색기 우클릭 메뉴에 AX Copilot 항목을 등록/해제합니다. /// 파일/폴더 선택 → 우클릭 → "AX Agent로 분석" 메뉴를 추가합니다. /// HKCU\Software\Classes 기반이므로 관리자 권한 불필요. /// public static class ShellExtensionService { private const string MenuKey = "AxCopilotShell"; private const string MenuLabel = "AX Agent로 분석"; /// 파일 우클릭 메뉴에 AX Agent 항목을 등록합니다. public static bool Register() { try { var exePath = Process.GetCurrentProcess().MainModule?.FileName ?? ""; if (string.IsNullOrEmpty(exePath)) return false; // 파일 우클릭: HKCU\Software\Classes\*\shell\AxCopilotShell RegisterContextMenu(@"Software\Classes\*\shell", exePath, "--analyze-file \"%1\""); // 폴더 우클릭: HKCU\Software\Classes\Directory\shell\AxCopilotShell RegisterContextMenu(@"Software\Classes\Directory\shell", exePath, "--analyze-folder \"%1\""); // 폴더 배경 우클릭: HKCU\Software\Classes\Directory\Background\shell\AxCopilotShell RegisterContextMenu(@"Software\Classes\Directory\Background\shell", exePath, "--analyze-folder \"%V\""); LogService.Info("셸 확장 등록 완료"); return true; } catch (Exception ex) { LogService.Error($"셸 확장 등록 실패: {ex.Message}"); return false; } } /// 등록된 우클릭 메뉴를 해제합니다. public static bool Unregister() { try { DeleteKey(@"Software\Classes\*\shell\" + MenuKey); DeleteKey(@"Software\Classes\Directory\shell\" + MenuKey); DeleteKey(@"Software\Classes\Directory\Background\shell\" + MenuKey); LogService.Info("셸 확장 해제 완료"); return true; } catch (Exception ex) { LogService.Error($"셸 확장 해제 실패: {ex.Message}"); return false; } } /// 셸 확장이 등록되어 있는지 확인합니다. public static bool IsRegistered() { try { using var key = Registry.CurrentUser.OpenSubKey(@"Software\Classes\*\shell\" + MenuKey); return key != null; } catch { return false; } } private static void RegisterContextMenu(string basePath, string exePath, string args) { var menuPath = $@"{basePath}\{MenuKey}"; using var menuKey = Registry.CurrentUser.CreateSubKey(menuPath); if (menuKey == null) return; menuKey.SetValue("", MenuLabel); menuKey.SetValue("Icon", $"\"{exePath}\",0"); using var cmdKey = Registry.CurrentUser.CreateSubKey($@"{menuPath}\command"); cmdKey?.SetValue("", $"\"{exePath}\" {args}"); } private static void DeleteKey(string path) { try { Registry.CurrentUser.DeleteSubKeyTree(path, false); } catch { } } /// 앱 시작 시 명령줄 인수를 처리합니다. public static (string Action, string Path)? ParseCommandLine(string[] args) { for (int i = 0; i < args.Length - 1; i++) { if (args[i] == "--analyze-file") return ("file", args[i + 1]); if (args[i] == "--analyze-folder") return ("folder", args[i + 1]); } return null; } }