How to obtain process of newly created IE8 window?

2019-02-24 23:21发布

问题:

When using .NET's Process.Start to run IE8's iexplore.exe, and if you already have another IE8 window open, then the iexplore.exe process that you just started will immediately exit, and its child process will attach to the main IE8 process. The result of this is your Process object will be linked to the "invoker" process that has already exited, but not the child process that is running. Anything you want to do to the Process object will result in an InvalidOperationException telling you the process has exited.

How do I obtain a Process object that is linked to the actual child IE8 process?

回答1:

As a workaround you may want to force IE to start a new instance (passing -nomerge command line argument):

Process objProcess = Process.Start("IEXPLORE.EXE", "-nomerge http://google.com/");


回答2:

I did it using a non-optimal solution - get a list of all iexplore.exe process IDs before running my iexplore.exe, then get the same list again after iexplore.exe is invoked. Do a diff between the two lists, the extra PID will be the one I've created. This of course won't work in certain cases (other apps or the user started extra iexplore.exe processes while the detection code is running, or IE did some kind of rearrange/recreation of processes as an internal maintenance task).