Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2009-05-10
2
Medium Priority
?
339 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
1 Comment
 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month20 days, 16 hours left to enroll

810 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