2011-12-09 57 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

它將工作... :)

相關問題