Solved

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

Posted on 2009-05-10
2
281 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Data Saving(2) 2 41
SQL Exceptions 3 37
Webform example, External Authentication Services, OAUTH, asp.net 4.6.2 1 10
C# Comparing String Arrays. 5 0
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

912 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

20 Experts available now in Live!

Get 1:1 Help Now