using System.Diagnostics; using PersonalToolBox.Models; namespace PersonalToolBox.Services; /// /// 进程执行服务,负责启动外部工具进程并处理异常 /// public class ProcessExecutionService : IProcessExecutionService { private readonly ILogService _logService; public ProcessExecutionService(ILogService logService) { _logService = logService; } public void Execute(ToolItem tool) { if (tool == null) { _logService.Error("尝试执行空工具项"); return; } if (!tool.IsValid) { _logService.Warning($"无法运行工具 \"{tool.Name}\",路径失效: {tool.ExecutablePath}"); return; } try { var startInfo = new ProcessStartInfo { FileName = tool.ExecutablePath, Arguments = tool.Arguments ?? string.Empty, UseShellExecute = true }; Process.Start(startInfo); _logService.Info($"成功启动: {tool.Name}"); } catch (Exception ex) { _logService.Error($"启动工具 \"{tool.Name}\" 失败: {ex.Message}"); } } }