/// <summary>
/// 获取正在运行的程序实例的命令行参数信息
/// </summary>
/// <returns></returns>
private static string GetRunningInstanceArgument()
{
var currentProcess = System.Diagnostics.Process.GetCurrentProcess();
var processName = currentProcess.ProcessName;
var processes = System.Diagnostics.Process.GetProcessesByName(processName);
var currentProcess = processes.FirstOrDefault();
var arguments = GetCommandLineArgsCore(currentProcess);
return arguments;
}
private static string GetCommandLineArgsCore(Process process)
{
using (var searcher = new ManagementObjectSearcher(
"SELECT CommandLine FROM Win32_Process WHERE ProcessId = " + process.Id))
using (var objects = searcher.Get())
{
var @object = objects.Cast<ManagementBaseObject>().SingleOrDefault();
return @object?["CommandLine"]?.ToString() ?? "";
}
}