?
Solved

C# code - How to know the existence and URL of the Active IE browser

Posted on 2009-05-10
2
Medium Priority
?
327 Views
Last Modified: 2012-05-06
Hi,

I am looking for code in C# where my question is:

Step 1: I open a IE window and browse to http://www.google.com.
Step2: I open the Visual Studio Team System 2008 and now want to write a C# code such that,
1. The code verifies that an active IE browser is open.
2. Output the URL of the active browser.

Can anyone please give me a C# code for this?
0
Comment
Question by:dev_qasource
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 24358042
You can use IE automation to get what you need.

Attached is a class that highlights Internet Explorer automation.  GetWindows returns a dictionary that stores the URL for each browser instance, and the HTML body text.
// Add a COM reference to 'Microsoft Internet Controls' to the project. 
// Add a .NET reference to Microsoft.mshtml to the project. 
 
// NOTE: 
// Don't add an Imports mshtml if you want your Intellisense to work. 
 
using System;
using System.Collections.Generic;
using SHDocVw;
 
public class InternetExplorerWindows
{
 
    public static Dictionary<string, string> GetWindows()
    {
        Dictionary<string, string> list = new Dictionary<string, string>();
        foreach (InternetExplorer window in new ShellWindows())
        {
 
            // Skip Windows Explorer instances, since the Document type is IShellFolderViewDual2. 
            if (window.Document is mshtml.HTMLDocument)
            {
                list.Add(window.LocationURL, ((mshtml.HTMLDocument)window.Document).body.innerHTML);
            }
        }
        return list;
    }
 
    public static Dictionary<string, string> GetIFrameHTML(string frameID)
    {
        Dictionary<string, string> list = new Dictionary<string, string>();
        foreach (InternetExplorer window in new ShellWindows())
        {
 
            // Skip Windows Explorer instances, since the Document type is IShellFolderViewDual2. 
            if (window.Document is mshtml.HTMLDocument)
            {
                mshtml.HTMLDocument doc = (mshtml.HTMLDocument)window.Document;
                if (doc.getElementById(frameID) != null)
                {
                    mshtml.HTMLIFrame frame = (mshtml.HTMLIFrame)doc.getElementById(frameID);
                    mshtml.HTMLWindow2 window2 = (mshtml.HTMLWindow2)frame.contentWindow;
                    mshtml.IHTMLDocument2 windowDocument = (mshtml.IHTMLDocument2)window2.document;
                    list.Add(window.LocationName, windowDocument.body.outerHTML);
                }
            }
        }
        return list;
    }
 
}

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question