init: 导入项目到 meswork Gitea
This commit is contained in:
185
DataMirrorTools_Simulator/ModelBasic/ExportModelHandler/Cmd.cs
Normal file
185
DataMirrorTools_Simulator/ModelBasic/ExportModelHandler/Cmd.cs
Normal file
@@ -0,0 +1,185 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Management;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FileExplorer
|
||||
{
|
||||
public class Cmd
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 是否终止调用CMD命令执行
|
||||
/// </summary>
|
||||
private static bool invokeCmdKilled = true;
|
||||
/// <summary>
|
||||
/// 获取或设置是否终止调用CMD命令执行
|
||||
/// </summary>
|
||||
public static bool InvokeCmdKilled
|
||||
{
|
||||
get { return invokeCmdKilled; }
|
||||
set
|
||||
{
|
||||
invokeCmdKilled = value;
|
||||
if (invokeCmdKilled)
|
||||
{
|
||||
if (p != null && !p.HasExited)
|
||||
{
|
||||
killProcess(p.Id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
private static Process p;
|
||||
private static Action<string> RefreshResult;
|
||||
|
||||
/// <summary>
|
||||
/// 调用CMD命令,执行指定的命令,并返回命令执行返回结果字符串
|
||||
/// </summary>
|
||||
/// <param name="cmdArgs">命令行</param>
|
||||
/// <param name="RefreshResult">刷新返回结果字符串的事件</param>
|
||||
/// <returns></returns>
|
||||
public static void InvokeCmd(string cmdArgs, Action<string> pRefreshResult = null)
|
||||
{
|
||||
InvokeCmdKilled = false;
|
||||
RefreshResult = pRefreshResult;
|
||||
if (p != null)
|
||||
{
|
||||
p.Close();
|
||||
p = null;
|
||||
}
|
||||
p = new Process();
|
||||
p.StartInfo.FileName = "cmd.exe";
|
||||
p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);
|
||||
p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
|
||||
p.StartInfo.UseShellExecute = false;
|
||||
p.StartInfo.RedirectStandardInput = true;
|
||||
p.StartInfo.RedirectStandardOutput = true;
|
||||
p.StartInfo.RedirectStandardError = true;
|
||||
p.StartInfo.CreateNoWindow = true;
|
||||
|
||||
p.Start();
|
||||
p.BeginErrorReadLine();
|
||||
p.BeginOutputReadLine();
|
||||
|
||||
p.StandardInput.WriteLine(cmdArgs);
|
||||
}
|
||||
/// <summary>
|
||||
/// 异步读取标准输出信息
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
static void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
|
||||
{
|
||||
if (e.Data != null)
|
||||
{
|
||||
if (e.Data.Length > 5)
|
||||
{
|
||||
if (e.Data.LastIndexOf("Total") > -1)// .f.Substring(5)== "Total")
|
||||
{
|
||||
|
||||
// // ModelBasic.ErrDef.iErrHandler.ErrInfoTransmit("【导出完成】 " + e.Data);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步读取错误消息
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
static void p_ErrorDataReceived(object sender, DataReceivedEventArgs e)
|
||||
{
|
||||
if ( e.Data != null)
|
||||
{
|
||||
// // ModelBasic.ErrDef.iErrHandler.ErrInfoTransmit("【导出错误】"+ e.Data);
|
||||
}
|
||||
else
|
||||
{
|
||||
// // ModelBasic.ErrDef.iErrHandler.ErrInfoTransmit("【导出错误】但是错误原因是空值!");
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 关闭指定进程ID的进程以及子进程(关闭进程树)
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
public static void FindAndKillProcess(int id)
|
||||
{
|
||||
killProcess(id);
|
||||
}
|
||||
/// <summary>
|
||||
/// 关闭指定进程名称的进程以及子进程(关闭进程树)
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
public static void FindAndKillProcess(string name)
|
||||
{
|
||||
foreach (Process clsProcess in Process.GetProcesses())
|
||||
{
|
||||
if ((clsProcess.ProcessName.StartsWith(name, StringComparison.CurrentCulture)) || (clsProcess.MainWindowTitle.StartsWith(name, StringComparison.CurrentCulture)))
|
||||
killProcess(clsProcess.Id);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 关闭进程树
|
||||
/// </summary>
|
||||
/// <param name="pid"></param>
|
||||
/// <returns></returns>
|
||||
private static bool killProcess(int pid)
|
||||
{
|
||||
Process[] procs = Process.GetProcesses();
|
||||
for (int i = 0; i < procs.Length; i++)
|
||||
{
|
||||
if (getParentProcess(procs[i].Id) == pid)
|
||||
killProcess(procs[i].Id);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Process myProc = Process.GetProcessById(pid);
|
||||
myProc.Kill();
|
||||
}
|
||||
//进程已经退出
|
||||
catch (Exception err)
|
||||
{
|
||||
LogNet.LogisTrac.WriteLog(typeof(Cmd), err);
|
||||
|
||||
;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取父进程ID
|
||||
/// </summary>
|
||||
/// <param name="Id"></param>
|
||||
/// <returns></returns>
|
||||
private static int getParentProcess(int Id)
|
||||
{
|
||||
int parentPid = 0;
|
||||
using (ManagementObject mo = new ManagementObject("win32_process.handle='" + Id.ToString(CultureInfo.InvariantCulture) + "'"))
|
||||
{
|
||||
try
|
||||
{
|
||||
mo.Get();
|
||||
}
|
||||
catch (Exception err)
|
||||
{
|
||||
LogNet.LogisTrac.WriteLog(typeof(Cmd), err);
|
||||
|
||||
return -1;
|
||||
}
|
||||
parentPid = Convert.ToInt32(mo["ParentProcessId"], CultureInfo.InvariantCulture);
|
||||
}
|
||||
return parentPid;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user