|
@@ -1,8 +1,8 @@
|
|
|
//************************************************************************
|
|
//************************************************************************
|
|
|
// https://github.com/yuzhengyang
|
|
// https://github.com/yuzhengyang
|
|
|
// author: yuzhengyang
|
|
// author: yuzhengyang
|
|
|
-// date: 2017.10.12 - 2017.10.12
|
|
|
|
|
-// desc: 启动进程工具
|
|
|
|
|
|
|
+// date: 2017.10.12 - 2018.4.27
|
|
|
|
|
+// desc: 进程工具
|
|
|
// Copyright (c) yuzhengyang. All rights reserved.
|
|
// Copyright (c) yuzhengyang. All rights reserved.
|
|
|
//************************************************************************
|
|
//************************************************************************
|
|
|
using Azylee.Core.DataUtils.CollectionUtils;
|
|
using Azylee.Core.DataUtils.CollectionUtils;
|
|
@@ -14,23 +14,12 @@ namespace Azylee.Core.ProcessUtils
|
|
|
{
|
|
{
|
|
|
public static class ProcessTool
|
|
public static class ProcessTool
|
|
|
{
|
|
{
|
|
|
- public static void StartProcess(string appFile)
|
|
|
|
|
- {
|
|
|
|
|
- try
|
|
|
|
|
- {
|
|
|
|
|
- if (File.Exists(appFile))
|
|
|
|
|
- {
|
|
|
|
|
- Process p = new Process();
|
|
|
|
|
- p.StartInfo.FileName = appFile;
|
|
|
|
|
- //p.StartInfo.Arguments = "";
|
|
|
|
|
- p.StartInfo.UseShellExecute = true;
|
|
|
|
|
- p.Start();
|
|
|
|
|
- p.WaitForInputIdle(3000);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- catch (Exception ex) { }
|
|
|
|
|
- }
|
|
|
|
|
- public static bool CheckProcessExists(string name)
|
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
|
+ /// 判断进程是否存在
|
|
|
|
|
+ /// </summary>
|
|
|
|
|
+ /// <param name="name">进程名</param>
|
|
|
|
|
+ /// <returns></returns>
|
|
|
|
|
+ public static bool IsExists(string name)
|
|
|
{
|
|
{
|
|
|
Process[] processes = Process.GetProcessesByName(name);
|
|
Process[] processes = Process.GetProcessesByName(name);
|
|
|
foreach (Process p in processes)
|
|
foreach (Process p in processes)
|
|
@@ -39,33 +28,30 @@ namespace Azylee.Core.ProcessUtils
|
|
|
}
|
|
}
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
- public static void KillProcess(string name)
|
|
|
|
|
- {
|
|
|
|
|
- try
|
|
|
|
|
- {
|
|
|
|
|
- Process[] processes = Process.GetProcessesByName(name);
|
|
|
|
|
- foreach (Process p in processes)
|
|
|
|
|
- {
|
|
|
|
|
- p.Kill();
|
|
|
|
|
- p.Close();
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- catch (Exception e) { }
|
|
|
|
|
- }
|
|
|
|
|
- public static void KillCurrentProcess()
|
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
|
+ /// 启动程序
|
|
|
|
|
+ /// </summary>
|
|
|
|
|
+ /// <param name="file">文件路径</param>
|
|
|
|
|
+ /// <param name="args">启动参数</param>
|
|
|
|
|
+ /// <returns></returns>
|
|
|
|
|
+ public static bool Start(string file, string args = "")
|
|
|
{
|
|
{
|
|
|
- Process current = Process.GetCurrentProcess();
|
|
|
|
|
- Process[] processes = Process.GetProcessesByName(current.ProcessName);
|
|
|
|
|
- foreach (Process process in processes)
|
|
|
|
|
|
|
+ if (File.Exists(file))
|
|
|
{
|
|
{
|
|
|
- if (process.Id == current.Id)
|
|
|
|
|
|
|
+ try
|
|
|
{
|
|
{
|
|
|
- process.Kill();
|
|
|
|
|
|
|
+ Process.Start(file, args);
|
|
|
|
|
+ return true;
|
|
|
}
|
|
}
|
|
|
|
|
+ catch { }
|
|
|
}
|
|
}
|
|
|
|
|
+ return false;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- public static bool Start(string file, string args = "")
|
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
|
+ /// 启动进程(定制启动配置)
|
|
|
|
|
+ /// </summary>
|
|
|
|
|
+ /// <param name="args"></param>
|
|
|
|
|
+ public static bool StartCustom(string file, string args = "")
|
|
|
{
|
|
{
|
|
|
try
|
|
try
|
|
|
{
|
|
{
|
|
@@ -73,7 +59,7 @@ namespace Azylee.Core.ProcessUtils
|
|
|
{
|
|
{
|
|
|
Process p = new Process();
|
|
Process p = new Process();
|
|
|
p.StartInfo.FileName = file;
|
|
p.StartInfo.FileName = file;
|
|
|
- p.StartInfo.Arguments = "";
|
|
|
|
|
|
|
+ p.StartInfo.Arguments = args;
|
|
|
p.StartInfo.UseShellExecute = true;
|
|
p.StartInfo.UseShellExecute = true;
|
|
|
p.Start();
|
|
p.Start();
|
|
|
p.WaitForInputIdle(3000);
|
|
p.WaitForInputIdle(3000);
|
|
@@ -83,38 +69,50 @@ namespace Azylee.Core.ProcessUtils
|
|
|
catch (Exception ex) { }
|
|
catch (Exception ex) { }
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
- public static bool SimpleStart(string file, string args = "")
|
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
|
+ /// 停止进程
|
|
|
|
|
+ /// </summary>
|
|
|
|
|
+ /// <param name="name">进程名</param>
|
|
|
|
|
+ public static void Kill(string name)
|
|
|
{
|
|
{
|
|
|
- if (File.Exists(file))
|
|
|
|
|
|
|
+ try
|
|
|
{
|
|
{
|
|
|
- try
|
|
|
|
|
|
|
+ Process[] processes = Process.GetProcessesByName(name);
|
|
|
|
|
+ foreach (Process p in processes)
|
|
|
{
|
|
{
|
|
|
- Process.Start(file, args);
|
|
|
|
|
- return true;
|
|
|
|
|
|
|
+ p.Kill();
|
|
|
|
|
+ p.Close();
|
|
|
}
|
|
}
|
|
|
- catch { }
|
|
|
|
|
}
|
|
}
|
|
|
- return false;
|
|
|
|
|
|
|
+ catch (Exception e) { }
|
|
|
}
|
|
}
|
|
|
- public static void Starts(string[] files)
|
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
|
+ /// 停止当前进程
|
|
|
|
|
+ /// </summary>
|
|
|
|
|
+ public static void KillCurrentProcess()
|
|
|
{
|
|
{
|
|
|
- if (ListTool.HasElements(files))
|
|
|
|
|
|
|
+ Process current = Process.GetCurrentProcess();
|
|
|
|
|
+ Process[] processes = Process.GetProcessesByName(current.ProcessName);
|
|
|
|
|
+ foreach (Process process in processes)
|
|
|
{
|
|
{
|
|
|
- foreach (var f in files)
|
|
|
|
|
|
|
+ if (process.Id == current.Id)
|
|
|
{
|
|
{
|
|
|
- if (!string.IsNullOrWhiteSpace(f))
|
|
|
|
|
- StartProcess(f);
|
|
|
|
|
|
|
+ process.Kill();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- public static void Kills(string[] pro)
|
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
|
+ /// 停止多个进程
|
|
|
|
|
+ /// </summary>
|
|
|
|
|
+ /// <param name="names"></param>
|
|
|
|
|
+ public static void Kills(string[] names)
|
|
|
{
|
|
{
|
|
|
- if (ListTool.HasElements(pro))
|
|
|
|
|
|
|
+ if (ListTool.HasElements(names))
|
|
|
{
|
|
{
|
|
|
- foreach (var p in pro)
|
|
|
|
|
|
|
+ foreach (var name in names)
|
|
|
{
|
|
{
|
|
|
- if (!string.IsNullOrWhiteSpace(p))
|
|
|
|
|
- KillProcess(p);
|
|
|
|
|
|
|
+ if (!string.IsNullOrWhiteSpace(name))
|
|
|
|
|
+ Kill(name);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|