123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- using System;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Threading;
- namespace Vinno.FIS.Sonopost.Common
- {
- public class ProcessHelper
- {
- /// <summary>
- /// 开启进程
- /// </summary>
- /// <param name="processPath"></param>
- /// <param name="args"></param>
- /// <returns></returns>
- public static int StartProcess(string processPath, string args = "")
- {
- if (!File.Exists(processPath))
- {
- return 0;
- }
- int retryCount = 0;
- int processId;
- bool exist;
- do
- {
- var p = new Process
- {
- StartInfo =
- {
- FileName = processPath,
- CreateNoWindow = true,
- WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory,
- UseShellExecute = false,
- WindowStyle = ProcessWindowStyle.Hidden,
- RedirectStandardInput = true,
- Verb = "runas",
- Arguments = args
- }
- };
- p.Start();
- processId = p.Id;
- var existApp = IsProcessExist(processId);
- exist = existApp != null;
- if (exist == false && retryCount < 5)
- {
- retryCount++;
- Thread.Sleep(1000);
- }
- } while (exist == false);
- return processId;
- }
- /// <summary>
- /// 结束进程
- /// </summary>
- /// <param name="processId"></param>
- /// <returns></returns>
- public static bool FinishProcess(int processId)
- {
- int count = 10;
- while (count > 0)
- {
- var existApp = IsProcessExist(processId);
- if (existApp != null)
- {
- count--;
- try
- {
- existApp.Kill();
- }
- catch
- {
- }
- Thread.Sleep(100);
- }
- else
- {
- return true;
- }
- }
- return false;
- }
- /// <summary>
- /// 结束所有这名称进程
- /// </summary>
- /// <param name="name"></param>
- /// <returns></returns>
- public static bool FinishProcessByName(string processName)
- {
- int count = 10;
- while (count > 0)
- {
- var existApps = Process.GetProcessesByName(processName).Where(x => x.ProcessName == processName);
- if (!existApps.Any())
- {
- return true;
- }
- foreach (var item in existApps)
- {
- try
- {
- item.Kill();
- }
- catch
- {
- }
- Thread.Sleep(100);
- }
- count--;
- }
- return false;
- }
- /// <summary>
- /// 检查进程是否存在
- /// </summary>
- /// <param name="processId"></param>
- /// <returns></returns>
- public static Process IsProcessExist(int processId)
- {
- try
- {
- var existApp = Process.GetProcessById(processId);
- if (existApp != null)
- {
- return existApp;
- }
- }
- catch (ArgumentException)
- {
- //ignore
- }
- return null;
- }
- /// <summary>
- /// 检查进程是否存在
- /// </summary>
- /// <param name="processName"></param>
- /// <returns></returns>
- public static bool IsProcessExistByName(string processName)
- {
- return Process.GetProcessesByName(processName).Any(x => x.ProcessName == processName);
- }
- /// <summary>
- /// 根据进程名得到唯一的进程Id
- /// </summary>
- /// <param name="processName"></param>
- /// <returns></returns>
- public static int GetProcessIdByName(string processName)
- {
- var existProcesses = Process.GetProcessesByName(processName).Where(x => x.ProcessName == processName);
- if (existProcesses.Count() == 1)
- {
- return existProcesses.FirstOrDefault().Id;
- }
- else
- {
- return 0;
- }
- }
- }
- }
|