/**
*┌──────────────────────────────────────────────────────────────┐
*│ 描 述:进程相关的工具类
*│ 作 者:执笔小白
*│ 版 本:1.2
*│ 创建时间:2022-10-13 10:40:56
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│ 命名空间: Common
*│ 类 名:ProcessHelper
*└──────────────────────────────────────────────────────────────┘
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Common
{
public class ProcessHelper
{
/// <summary>
/// 获取当前进程信息
/// </summary>
/// <returns></returns>
public static Process GetCurrentProcess()
{
return Process.GetCurrentProcess();
}
/// <summary>
/// 获取当前进程名
/// </summary>
/// <returns></returns>
public static string GetCurrentProcessName()
{
return Process.GetCurrentProcess().ProcessName;
}
/// <summary>
/// 开启进程By程序启动文件路径
/// </summary>
/// <param name="processName">进程名</param>
/// <param name="arguments">设置启动程序时使用的命令行自变量</param>
/// <param name="windowStyle">启动时应用的窗口状态</param>
public static Process StartProcess(string processPath, string arguments = "", ProcessWindowStyle windowStyle = ProcessWindowStyle.Minimized)
{
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = processPath;
info.Arguments = arguments;
info.WindowStyle = windowStyle;
return Process.Start(info);
}
/// <summary>
/// 正常关闭进程By进程名
/// </summary>
/// <param name="processName">进程名</param>
public static void CloseProcess(string processName)
{
foreach (Process process in Process.GetProcessesByName(processName))
{
try
{
if (!process.CloseMainWindow()) // 通知正常关闭
{
process.Kill(); // 强制结束
process.WaitForExit(); // 等待退出完成
}
}
catch { }
}
}
/// <summary>
/// 强制杀死进程By进程名
/// </summary>
/// <param name="processName">进程名</param>
public static void KillProcess(string processName)
{
foreach (Process process in Process.GetProcessesByName(processName))
{
try
{
process.Kill(); // 强制结束
process.WaitForExit(); // 等待退出完成
}
catch { }
}
}
}
}
标签:Process,C#,System,process,进程,using,工具,public
From: https://www.cnblogs.com/qq2806933146xiaobai/p/17222128.html