C#如何获取一个正在运行的EXE中的参数值

2024-12-17 10:30:51
推荐回答(1个)
回答1:

用这段代码. 这个就是用wmi查询运行中的进程,最后把名字标题路径命令行参数塞到一个dataTable里面,然后你就在这个table里面找你要的进程就行了,每行是一个进程

# public static DataTable GetRunningProcesses()
# {
# //One way of constructing a query
# string wmiClass = "Win32_Process";
# string condition = "";
# string[] queryProperties = new string[] { "Name", "ProcessId", "Caption", "ExecutablePath", "CommandLine" };
# SelectQuery wmiQuery = new SelectQuery(wmiClass, condition, queryProperties);
# ManagementScope scope = new System.Management.ManagementScope(@"\\.\root\CIMV2");
#
# ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, wmiQuery);
# ManagementObjectCollection runningProcesses = searcher.Get();
#
# DataTable queryResults = new DataTable();
# queryResults.Columns.Add("Name", Type.GetType("System.String"));
# queryResults.Columns.Add("ProcessId", Type.GetType("System.Int32"));
# queryResults.Columns.Add("Caption", Type.GetType("System.String"));
# queryResults.Columns.Add("Path", Type.GetType("System.String"));
# queryResults.Columns.Add("CommandLine", Type.GetType("System.String"));
#
# foreach(ManagementObject obj in runningProcesses)
# {
# DataRow row = queryResults.NewRow();
# row["Name"] = obj["Name"].ToString();
# row["ProcessId"] = Convert.ToInt32(obj["ProcessId"]);
# if (obj["Caption"]!= null)
# row["Caption"] = obj["Caption"].ToString();
# if (obj["ExecutablePath"]!= null)
# row["Path"] = obj["ExecutablePath"].ToString();
# if(obj["CommandLine"] != null)
# row["CommandLine"] = obj["CommandLine"].ToString();
# queryResults.Rows.Add( row );
# }
# return queryResults;
# }