asmrobot
2019-11-25 2aeab450471cb80b59002da7da80faf251a0c4f4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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
    {
        /// <summary>
        /// 是否存在指定进度名
        /// </summary>
        /// <param name="processName"></param>
        /// <returns></returns>
        public static bool ExistsProcessByName(string processName)
        {
            Process[] processes = Process.GetProcessesByName(processName);
            if (processes.Length <= 0)
            {
                return false;
            }
 
            return true;
        }
 
        /// <summary>
        /// 结束进程
        /// </summary>
        /// <param name="processName"></param>
        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();
                }
            }
        }
 
        /// <summary>
        /// wegame是否已启动
        /// </summary>
        /// <returns></returns>
        public static bool ExistsWeGame()
        {
            return ExistsProcessByName("tgp_daemon");
        }
 
        /// <summary>
        /// dnf
        /// </summary>
        /// <returns></returns>
        public static bool ExistsDnf()
        {
            return ExistsProcessByName("dnf");
        }
 
        /// <summary>
        /// 关闭wegame
        /// </summary>
        public static void CloseWeGame()
        {
            KillProcessByName("tgp_daemon");
        }
 
        /// <summary>
        /// 关闭dnf
        /// </summary>
        public static void CloseDnf()
        {
            KillProcessByName("dnf");
        }
    }
}