using System;
using System.Diagnostics;
using System.IO;
using System.ComponentModel; // 用于Win32Exception
public class ProcessManager
{
    public static bool IsProcessRunning(string targetPath)
    {
        string fullTargetPath = Path.GetFullPath(targetPath).TrimEnd('\\');
        foreach (var process in Process.GetProcesses())
        {
            try
            {
                if (process.MainModule == null) continue;
                
                string processPath = Path.GetFullPath(process.MainModule.FileName).TrimEnd('\\');
                
                if (string.Equals(processPath, fullTargetPath, StringComparison.OrdinalIgnoreCase))
                {
                    return true;
                }
            }
            catch (Exception ex) when (ex is Win32Exception || ex is InvalidOperationException)
            {
                // 跳过无权限访问的进程
            }
            finally
            {
                process.Dispose();
            }
        }
        return false;
    }
    public static void KillProcessByPath(string targetPath)
    {
        string fullTargetPath = Path.GetFullPath(targetPath).TrimEnd('\\');
        bool found = false;
        foreach (var process in Process.GetProcesses())
        {
            try
            {
                if (process.MainModule == null) continue;
                
                string processPath = Path.GetFullPath(process.MainModule.FileName).TrimEnd('\\');
                
                if (string.Equals(processPath, fullTargetPath, StringComparison.OrdinalIgnoreCase))
                {
                    process.Kill();
                    process.WaitForExit(3000); // 等待最多3秒
                    found = true;
                    Console.WriteLine($"已终止进程: {process.ProcessName} (PID: {process.Id})");
                }
            }
            catch (Win32Exception ex)
            {
                Console.WriteLine($"权限不足,无法终止进程 {process.ProcessName}: {ex.Message}");
            }
            catch (InvalidOperationException)
            {
                // 进程已退出,忽略
            }
            catch (Exception ex)
            {
                Console.WriteLine($"终止进程 {process.ProcessName} 时出错: {ex.Message}");
            }
            finally
            {
                process.Dispose();
            }
        }
        if (!found)
        {
            Console.WriteLine($"未找到运行中的进程: {Path.GetFileName(targetPath)}");
        }
    }
    // 使用示例
    public static void Main()
    {
        string exePath = @"C:\Program Files\MyApp\MyProgram.exe";
        
        if (IsProcessRunning(exePath))
        {
            Console.WriteLine("程序正在运行,即将强制终止...");
            KillProcessByPath(exePath);
            
            // 二次验证
            if (!IsProcessRunning(exePath))
            {
                Console.WriteLine("程序已成功终止");
            }
            else
            {
                Console.WriteLine("程序终止失败");
            }
        }
        else
        {
            Console.WriteLine("程序未运行");
        }
    }
}