12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Security.Principal;
- using System.Text;
- using System.Threading.Tasks;
- namespace Ips.Library.Basic
- {
- public class ProcessUtil
- {
- public static void KillProcessByName(string procName)
- {
- procName = Path.GetFileNameWithoutExtension(procName);
- var procList = Process.GetProcessesByName(procName);
- foreach (var proc in procList)
- {
- proc.Kill();
- var success = proc.WaitForExit(3000);
- if (!success)
- {
- throw new Exception($"停止进程失败:{proc.MainModule.FileName}");
- }
- }
- }
- /// <summary>
- /// 检查当前正在运行的主程序是否是在 Debug 配置下编译生成的。
- /// </summary>
- public static bool IsDebug
- {
- get
- {
- if (_isDebugMode == null)
- {
- var assembly = Assembly.GetEntryAssembly();
- if (assembly == null)
- {
- // 由于调用 GetFrames 的 StackTrace 实例没有跳过任何帧,所以 GetFrames() 一定不为 null。
- assembly = new StackTrace().GetFrames().Last().GetMethod().Module.Assembly;
- }
- var debuggableAttribute = assembly.GetCustomAttribute<DebuggableAttribute>();
- _isDebugMode = debuggableAttribute.DebuggingFlags
- .HasFlag(DebuggableAttribute.DebuggingModes.EnableEditAndContinue);
- }
- return _isDebugMode.Value;
- }
- }
- private static bool? _isDebugMode;
- }
- }
|