简体   繁体   English

如何在C#中获取单个进程的多个实例详细信息?

[英]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可在实现IEnumerableIEnumerable<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.

相关问题 如何使用c#.net获取计算机中正在运行的进程的详细信息? - How to get details of running process in a computer using c# .net? 如何在C#中为多个值运行单个进程? - How to run single process for multiple values in c#? c#:具有多个应用程序域的单个进程VS多个进程 - c# : single process with multiple app domain VS multiple process 如何将面板的单个实例多次添加到单个 FlowLayoutPanel(C# 桌面应用程序)? - How to add a single instance of Panel multiple time to a single FlowLayoutPanel(C# desktop application)? C# Winforms如何获取CheckedListBox的详细信息? - C# Winforms how to get details of CheckedListBox? 如何从可执行文件中获取图标只有C#中的Process实例 - How can I get the icon from the executable file only having an instance of it's Process in C# 使用C#中的Acrobat DC的进程句柄获取应用程序实例 - Get App Instance using process handle of Acrobat DC in C# 如何在C#的单个进程中使用线程启动exe appln的多个实例? - How to launch multiple instances of an exe appln using threads in a single process in c#? 如何在C#中使用单个Crystal Report Viewer打印Crystal Report的多个实例 - How to print multiple instance of crystal report using single Crystal Report Viewer in c# 如何在 C# 中的多个上下文中使用 class 的单个实例? - How do I utilize a single instance of a class over multiple contexts in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM