You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.1 KiB

using System.Diagnostics;
using System.Runtime.InteropServices;
namespace BrilliantSightClient.Model.Helper;
public class ProcessHelper
{
private static extern bool CloseHandle(IntPtr hObject);
// 进程访问权限
private const int PROCESS_TERMINATE = 0x0001;
// 根据进程名查找并终止进程
public static void TerminateProcessByName(string processName)
{
try
{
Process[] processes = Process.GetProcessesByName(processName);
foreach (Process process in processes)
{
try
{
process.Kill();
process.WaitForExit();
}
catch (Exception ex)
{
// 可以选择记录错误但不显示给用户
Debug.WriteLine($"结束进程出错: {ex.Message}");
}
}
}
catch (Exception ex)
{
Console.WriteLine($"查找进程时出错: {ex.Message}");
}
}
}