c#获取所有打开的窗口标题

时间:2015-09-04 11:05:23

标签: c# windows internet-explorer

我正在编写一个程序来获取Internet Explorer的所有Windows标题。假设如果打开了2个Internet Explorer窗口,我的程序应显示标题&获取所有Internet Explorer窗口的句柄。这是我的程序

using System.Diagnostics;

Process[] processlist = Process.GetProcesses();

foreach (Process process in processlist)
{
    if (!String.IsNullOrEmpty(process.MainWindowTitle))
    {
        Console.WriteLine("Process: {0} ID: {1} Window title: {2}", process.ProcessName, process.Id, process.MainWindowTitle);
    }
}

此程序正在运行,但只能获得单个窗口的文本。当我看到,当打开2到3个Internet Explorer窗口时,有3到4个iexplore.exe正在运行。在查看其他答案时,他们只使用一个流程,但有iexplore.exe个存在。如何获取打开的Internet Explorer窗口的所有句柄?

由于

1 个答案:

答案 0 :(得分:0)

您可以使用ManagmenObjectSearcher从特定进程中获取所有父对象

 private void Form1_Load(object sender, EventArgs e)
    {

        Process[] processlist = Process.GetProcesses();

        foreach (Process process in processlist)
        {
            if (!String.IsNullOrEmpty(process.MainWindowTitle))
            {
                if (process.Id == 4160)//this is chrome id for example/
                {
                    //Console.WriteLine("Process: {0} ID: {1} Window title: {2}", process.ProcessName, process.Id, process.MainWindowTitle);
                    List<Process> a = GetChild(process);

                    foreach( Process p in a)
                    {
                        Console.WriteLine("Process: {0} ID: {1} Window title: {2}", process.ProcessName, process.Id, process.MainWindowTitle);
                    }
                }
            }
        }
    }

    public  static List<Process> GetChild(Process process)
    {
        List<Process> children = new List<Process>();
        ManagementObjectSearcher mos = new ManagementObjectSearcher(String.Format("Select * From Win32_Process Where ParentProcessID={0}", process.Id));

        foreach (ManagementObject mo in mos.Get())
        {
            children.Add(Process.GetProcessById(Convert.ToInt32(mo["ProcessID"])));
        }

        return children;
    }

古德勒克。