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

C#如何获取一个正在运行的EXE中的参数值,第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

# }

把cmd.exe 替换成bat文件核备握名,

"/c dir" 替换成文件名后面的滚歼参数

供参改庆考:

System.Diagnostics.Process p = new System.Diagnostics.Process()

p.StartInfo=new System.Diagnostics.ProcessStartInfo("cmd.exe")

p.StartInfo.Arguments = "/c dir"

p.StartInfo.RedirectStandardOutput = true

p.StartInfo.UseShellExecute = false

p.Start()

p.WaitForExit()

string output=p.StandardOutput.ReadToEnd()

MessageBox.Show(output)


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12559886.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-26
下一篇 2023-05-26

发表评论

登录后才能评论

评论列表(0条)

保存