Friday, April 24, 2009

IE C# 2

http://www.codeproject.com/KB/vb/kirangoka.aspx

Introduction

Microsoft Internet Explorer comes with a fairly comprehensive, although sparsely documented, Object Model. If you've used the Web Browser control in Access, you are already familiar with the capabilities of IE's Object Model. All of the functionality in IE's object model (not counting external support, like scripting support etc.) is provided by the following two dlls:

* shdocvw.dll (Microsoft Internet Controls)
* mshtml.tlb (Microsoft HTML Object Library)

You can automate IE to save a HTML file locally , inspect all the elements, and parse out a particular item at runtime.

Here's some sample code that automate through Internet Explorer windows login into the rediffmail.com, if the user name and password are valid.

First the application opens the http://rediff.com site. It types the user name and password at specified location and click the submit button so that it goes to inbox page. It also opens the compose page for the particular user.

The application extensively uses shdocvw.InternetExplorer object and mshtml.Document object.
Collapse

Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

Collapse

Dim wbBrowser As New SHDocVw.InternetExplorer wbBrowser.Visible = True
wbBrowser.Navigate("http://www.rediff.com", Nothing, Nothing, Nothing, Nothing) Do
Loop Until Not wbBrowser.Busy
LoginIntoSite(wbBrowser)
OpennComposePage(wbBrowser)

Collapse

End Sub

Public Sub LoginIntoSite(ByRef wbBrowser As SHDocVw.InternetExplorer)

Dim HTMLDoc As mshtml.HTMLDocument

Do
Loop Until Not wbBrowser.Busy

HTMLDoc = wbBrowser.Document

Dim iHTMLCol As IHTMLElementCollection
Dim iHTMLEle As IHTMLElement
Dim str, userName, passwd As String


iHTMLCol = HTMLDoc.getElementsByTagName("input")



' Type the user name in the username text box
For Each iHTMLEle In iHTMLCol
If Not iHTMLEle.getAttribute("name") Is Nothing Then
str = iHTMLEle.getAttribute("name").ToString
If str = "login" Then
iHTMLEle.setAttribute("value", "")
Exit For
End If
End If
Next

' Type the password in the password text box
For Each iHTMLEle In iHTMLCol
If Not iHTMLEle.getAttribute("name") Is Nothing Then
str = iHTMLEle.getAttribute("name").ToString
If str = "passwd" Then
iHTMLEle.setAttribute("value", "")
Exit For
End If
End If
Next

' Press the submit button
For Each iHTMLEle In iHTMLCol
If Not iHTMLEle.getAttribute("name") Is Nothing Then
If iHTMLEle.outerHTML = " " height=21 width=26 " & _
"src=""http://im.rediff.com/" & _
"uim/rm_go_but.gif"" border=0>" Then
iHTMLEle.click()
Exit For
End If
End If
Next

Do
Loop Until Not wbBrowser.Busy





End Sub

Public Sub OpenComposePage(ByRef wbBrowser As SHDocVw.InternetExplorer)

Dim HTMLDoc1 As mshtml.HTMLDocument
Dim iHtmlCol As IHTMLElementCollection
Dim iHtmlEle As IHTMLElement

Do
Loop Until Not wbBrowser.Busy

HTMLDoc1 = mshtml.HTMLDocument



iHtmlCol = HTMLDoc1.getElementsByTagName("a")

' Press the anchor tag to open compose page
For Each iHtmlEle In iHtmlCol
If Not iHtmlEle.outerText Is Nothing Then
If iHtmlEle.outerText.ToLower = "write mail".ToLower Then
iHtmlEle.click()
Exit For
End If
End If
Next

Do
Loop Until Not wbBrowser.Busy
End Sub

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

IE C#

http://www.omegacoder.com/?p=63

In this article I describe how to get the current URL in Internet Explorer and the current directory in Windows Explorer. This works in .Net 2 and IE 7. I am showing both at the same time because they are similar in methods of getting the address of the items, whether its a hard drive or a URL.

First step requires the access ShellWindows object which represents a collection of open windows in the system. To access that object which resides in the SHDocVw namespace we need to import a Com library Microsoft Internet Controls in to the project:

image

With that we can access the ShellWindows object and begin our work. Here is the code sample

using System.IO;



...



SHDocVw.ShellWindows shellWindows

= new SHDocVw.ShellWindowsClass();



string filename;



foreach (SHDocVw.InternetExplorer ie in shellWindows)

{

filename

= Path.GetFileNameWithoutExtension(ie.FullName).ToLower();



if (filename.Equals("iexplore"))

Console.WriteLine("Web Site : {0}", ie.LocationURL);



if (filename.Equals("explorer"))

Console.WriteLine("Hard Drive : {0}", ie.LocationURL);



}

Thanks to the magic of ShellWindows we are able to see all active windows. We discriminate and get the names of the windows we are interested in and wala here is the output:

Hard Drive : file:///C:/Work/Net2
Web Site : http://www.omegacoder.com/