[英]How to get multiple instance details of single process in c#?
In my desktop triplE application is running. 在我的桌面上,triplE应用程序正在运行。 Its process name is "Orig_sysdsner".
它的进程名称是“ Orig_sysdsner”。 Here single process is creating but multiple instances are creating where each instance title will be unique.
这里创建单个进程,但是创建多个实例,每个实例标题将是唯一的。 Now i want to get all instances of this process with instance name.
现在,我想使用实例名称获取此过程的所有实例。 How to do it in C#?
如何在C#中完成?
Below function i have tried but getting last opened instance details. 下面的功能我已经尝试过,但获取上次打开的实例详细信息。
public static List<string> GetDGDesignEditorRunningInstance()
{
List<string> runningInstanceList = new List<string>();
List<Process> processList = Process.GetProcessesByName("Orig_sysdsner").ToList();
try
{
foreach (Process process in processList)
{
//string instanceName = process.MainModule.FileName;
string instanceName = process.MainWindowTitle;
runningInstanceList.Add(instanceName);
}
}
catch (Exception ex)
{
throw ex;
}
return runningInstanceList;
}
A process may not have a window title. 进程可能没有窗口标题。 If I execute this :
如果我执行此操作:
var runningInstanceList=Process.GetProcessesByName("Chrome")
.Select(proc=>proc.MainWindowTitle)
.ToList();
I'll get back 16 entries and only one of them will have a MainWindowTitle. 我将取回16个条目,只有其中一个具有MainWindowTitle。 Chrome uses tabs and background processes that don't show any title.
Chrome使用不显示任何标题的标签和后台进程。
If I use MicrosoftEdgeCP
I'll get back 17 strings, all with the same title, Microsoft Edge
. 如果使用
MicrosoftEdgeCP
我将取回17个字符串,所有字符串均具有相同的标题Microsoft Edge
。
BTW you don't need the call to .ToList()
to iterate over the array returned by GetProcessByName
. 顺便说一句,您不需要调用
.ToList()
即可遍历GetProcessByName
返回的数组。 foreach
works on anything that implements IEnumerable
or IEnumerable<T>
. foreach
可在实现IEnumerable
或IEnumerable<T>
。 You can use LINQ to select the items you need so you can get rid of the loop and adding the titles to the result list one by one, eg: 您可以使用LINQ选择所需的项目,这样就可以摆脱循环并将标题逐一添加到结果列表中,例如:
var runningInstanceList=Process.GetProcessesByName("Chrome")
.Select(proc=>new { proc.Id,
proc.ProcessName,
proc.MainWindowTitle
})
.ToList();
Will return the unique process ID , the name and window title if it exists. 将返回唯一的进程ID,名称和窗口标题(如果存在)。
You can use Process Explorer and add the Window Title
to the process list to see what title is reported by each application. 您可以使用Process Explorer并将
Window Title
添加到进程列表中,以查看每个应用程序报告的标题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.