【问题标题】:How to get the URL of the Internet explorer tabs with PID of each tab?如何使用每个选项卡的 PID 获取 Internet Explorer 选项卡的 URL?
【发布时间】:2011-12-09 12:51:13
【问题描述】:

我的应用程序使用特定 URL 触发 Web 浏览器。 在我的程序结束后,我想关闭我打开的网页/标签..

通过调用带有参数 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 ...并且只杀死那个标签??

我得到了这个帮助 Access is denied - when trying to get the url (text) from address bar's handle

使用 SHDocVw; . .

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

【问题讨论】:

    标签: c# internet-explorer


    【解决方案1】:

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

    【讨论】:

    • 很好的信息,但你为什么要自言自语? :o/
    • Coz evryon 看过它,但没有人回应 :) 直到我自己找到了解决方案并分享了 tat :P
    • 使用这个代码会抛出两个错误。应该使用“ShellWindows()”而不是“ShellWindowsClass()”。
    【解决方案2】:

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

    向项目添加引用“Microsoft Internet Controls”。

    这段代码是

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

    生成exe和Interop.SHDocVw.dll

    它会起作用的...:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-14
      • 2014-01-15
      • 2013-06-21
      • 2023-03-10
      • 2020-06-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多