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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Microsoft.Win32;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace FileExplorer
|
||||
{
|
||||
public class GetSystemIcon
|
||||
{
|
||||
/// <summary>
|
||||
/// 依据文件名读取图标,若指定文件不存在,则返回空值。
|
||||
/// </summary>
|
||||
/// <param name="fileName"></param>
|
||||
/// <returns></returns>
|
||||
public static Icon GetIconByFileName(string fileName)
|
||||
{
|
||||
if (fileName == null || fileName.Equals(string.Empty)) return null;
|
||||
if (!File.Exists(fileName)) return null;
|
||||
|
||||
SHFILEINFO shinfo = new SHFILEINFO();
|
||||
//Use this to get the small Icon
|
||||
Win32.SHGetFileInfo(fileName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_LARGEICON);
|
||||
//The icon is returned in the hIcon member of the shinfo struct
|
||||
System.Drawing.Icon myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon);
|
||||
return myIcon;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 给出文件扩展名(.*),返回相应图标
|
||||
/// 若不以"."开头则返回文件夹的图标。
|
||||
/// </summary>
|
||||
/// <param name="fileType"></param>
|
||||
/// <param name="isLarge"></param>
|
||||
/// <returns></returns>
|
||||
public static Icon GetIconByFileType(string fileType, bool isLarge)
|
||||
{
|
||||
if (fileType == null || fileType.Equals(string.Empty)) return null;
|
||||
|
||||
RegistryKey regVersion = null;
|
||||
string regFileType = null;
|
||||
string regIconString = null;
|
||||
string systemDirectory = Environment.SystemDirectory + "\\";
|
||||
|
||||
if (fileType[0] == '.')
|
||||
{
|
||||
//读系统注册表中文件类型信息
|
||||
regVersion = Registry.ClassesRoot.OpenSubKey(fileType, true);
|
||||
if (regVersion != null)
|
||||
{
|
||||
regFileType = regVersion.GetValue("") as string;
|
||||
regVersion.Close();
|
||||
regVersion = Registry.ClassesRoot.OpenSubKey(regFileType + @"\DefaultIcon", true);
|
||||
if (regVersion != null)
|
||||
{
|
||||
regIconString = regVersion.GetValue("") as string;
|
||||
regVersion.Close();
|
||||
}
|
||||
}
|
||||
if (regIconString == null)
|
||||
{
|
||||
//没有读取到文件类型注册信息,指定为未知文件类型的图标
|
||||
regIconString = systemDirectory + "shell32.dll,0";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//直接指定为文件夹图标
|
||||
regIconString = systemDirectory + "shell32.dll,3";
|
||||
}
|
||||
string[] fileIcon = regIconString.Split(new char[] { ',' });
|
||||
if (fileIcon.Length != 2)
|
||||
{
|
||||
//系统注册表中注册的标图不能直接提取,则返回可执行文件的通用图标
|
||||
fileIcon = new string[] { systemDirectory + "shell32.dll", "2" };
|
||||
}
|
||||
Icon resultIcon = null;
|
||||
try
|
||||
{
|
||||
//调用API方法读取图标
|
||||
int[] phiconLarge = new int[1];
|
||||
int[] phiconSmall = new int[1];
|
||||
uint count = Win32.ExtractIconEx(fileIcon[0], Int32.Parse(fileIcon[1]), phiconLarge, phiconSmall, 1);
|
||||
IntPtr IconHnd = new IntPtr(isLarge ? phiconLarge[0] : phiconSmall[0]);
|
||||
resultIcon = Icon.FromHandle(IconHnd);
|
||||
}
|
||||
catch(Exception err)
|
||||
{
|
||||
LogNet.LogisTrac.WriteLog(typeof(GetSystemIcon), err);
|
||||
|
||||
}
|
||||
return resultIcon;
|
||||
}
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct SHFILEINFO
|
||||
{
|
||||
public IntPtr hIcon;
|
||||
public IntPtr iIcon;
|
||||
public uint dwAttributes;
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
|
||||
public string szDisplayName;
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
|
||||
public string szTypeName;
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// 定义调用的API方法
|
||||
/// </summary>
|
||||
class Win32
|
||||
{
|
||||
public const uint SHGFI_ICON = 0x100;
|
||||
public const uint SHGFI_LARGEICON = 0x0; // 'Large icon
|
||||
public const uint SHGFI_SMALLICON = 0x1; // 'Small icon
|
||||
|
||||
[DllImport("shell32.dll")]
|
||||
public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
|
||||
[DllImport("shell32.dll")]
|
||||
public static extern uint ExtractIconEx(string lpszFile, int nIconIndex, int[] phiconLarge, int[] phiconSmall, uint nIcons);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user