Solved

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

Posted on 2009-05-10
2
270 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
2 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now