using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RichCreator.Utility.Utilitys
{
public class ProcessUtils
{
///
/// 是否存在指定进度名
///
///
///
public static bool ExistsProcessByName(string processName)
{
Process[] processes = Process.GetProcessesByName(processName);
if (processes.Length <= 0)
{
return false;
}
return true;
}
///
/// 结束进程
///
///
public static void KillProcessByName(string processName)
{
Process[] processes = Process.GetProcessesByName(processName);
if (processes.Length <= 0)
{
return;
}
Process process = new Process();//创建进程对象
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";//设定需要执行的命令
startInfo.Arguments = "/C taskkill /T /F /IM " + processName+".exe";//“/C”表示执行完命令后马上退出
startInfo.UseShellExecute = false;//不使用系统外壳程序启动
startInfo.RedirectStandardInput = false;//不重定向输入
startInfo.RedirectStandardOutput = true; //重定向输出
startInfo.CreateNoWindow = true;//不创建窗口
process.StartInfo = startInfo;
try
{
if (process.Start())//开始进程
{
process.WaitForExit(50000); //等待进程结束,等待时间为指定的毫秒
string result=process.StandardOutput.ReadToEnd();
Console.WriteLine(result);
}
}
finally
{
if (process != null)
{
process.Close();
}
}
}
///
/// wegame是否已启动
///
///
public static bool ExistsWeGame()
{
return ExistsProcessByName("tgp_daemon");
}
///
/// dnf
///
///
public static bool ExistsDnf()
{
return ExistsProcessByName("dnf");
}
///
/// 关闭wegame
///
public static void CloseWeGame()
{
KillProcessByName("tgp_daemon");
}
///
/// 关闭dnf
///
public static void CloseDnf()
{
KillProcessByName("dnf");
}
}
}