Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
341 views
in Technique[技术] by (71.8m points)

c# - How to get the URL of the Internet explorer tabs with PID of each tab?

I have my application which triggers Web Browser with specific URL . After my program ends i want to close the web pages/tabs which i have opened..

By calling an EXE file with parameters a. Process Name b. String present in the URL

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

I used C# approach ...

I am able to find the process ID of all the tabs..

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

Currently i can get only Window Title of the process....and in IE8 only one window title of main process is visible..

Provided i have the pids of each tabs,How to find the URL of the tab ...and kill only that tab ??

I got this help from Access is denied - when trying to get the url (text) from address bar's handle

using SHDocVw; . .

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In IE7 and later versions, below code will kill only the tab which has matching string in its URL.

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

To focus a specific tab the code is :

   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);
        }
   }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...