2011-12-09 58 views
5

我有我的应用程序触发带有特定URL的Web浏览器。 我的程序结束后,我想关闭我打开的网页/选项卡。如何获取每个选项卡的PID的Internet Explorer选项卡的URL?

通过调用带有参数a的EXE文件。进程名称b。字符串出现在URL

How to kill firefox child process/tab from Java/C++

我用C#方法详细的问题...

我能找到的所有标签的进程ID ..

foreach (Process theprocess in processlist) { 
    if (theprocess.ProcessName == "iexplore") { 
     Console.WriteLine("Process: {0}\tID: {1}\tWindow name: {2}", 
      theprocess.ProcessName, theprocess.Id, theprocess.MainWindowTitle 
     ); 
    } 
} 

目前我只能得到过程的窗口标题....并且在IE8中只有主窗口的一个窗口标题可见..

箴言我想我有每个选项卡的pid,如何找到选项卡的URL ...并只杀死该选项卡?

我使用SHDOCVW Access is denied - when trying to get the url (text) from address bar's handle

此帮助; 。 。

foreach(InternetExplorer ieInst in new ShellWindowsClass()) Console.WriteLine(ieInst.LocationURL);

回答

8

在IE7及更高版本,下面的代码会只杀死其URL中具有匹配字符串的选项卡。

foreach (SHDocVw.InternetExplorer ieInst in new SHDocVw.ShellWindows()) 
    { 
     String url = ieInst.LocationURL; 
     if (url.Contains("google")) 
     { 
      ieInst.Quit(); 
     } 
    } 

聚焦于特定标签的代码是:

foreach (SHDocVw.InternetExplorer ieInst in new SHDocVw.ShellWindows()) 
    { 
     String url = ieInst.LocationURL; 
     if (url.Contains("google")) 
     { 
      int val = ieInst.HWND; 
      IntPtr hwnd = new IntPtr(val); 
      ShowWindow(hwnd, SW_MAXIMISE); 
      SetForegroundWindow(hwnd); 
     } 
    } 
+0

伟大的信息,但是为什么你在说你自己? :o/ –

+2

Coz evryon查看它并且没有任何机构回应:)直到我自己找到解决方案,并共享tat:P –

+2

使用此代码将引发两个错误。应该使用“ShellWindows()”而不是“ShellWindowsClass()”。 –

3

有一种方法可以获取每个IExplorer实例的URL!

为项目添加参考“Microsoft Internet Controls”。

的一段代码是

**foreach (SHDocVw.InternetExplorer ieInst in new SHDocVw.ShellWindowsClass()) 
     { 
      System.Console.WriteLine(ieInst.LocationURL); 
     }** 

生成的exe和Interop.SHDocVw.dll

它将工作... :)

相关问题