Tuesday, November 28, 2006

How to open web page from windows application (C#/NET)

How to open web page from windows application (C#/NET)
Introduction
Some time it’s requiring to open IE from dot net application. For example: If you want your window application opens the web (.aspx) page. Then you require some code that should call web application and start the web project.
You can open any web site through this application like: www.yahoomail.com, www.gmail.com etc.
Follow the three simple step to create this application.
1) Give the web reference of Microsoft.mshtml.dll and Interop.SHDocVw.dll DLLs to your project
Path:

a) C:\WINDOWS\assembly\GAC\Microsoft.mshtml

\7.0.3300.0__b03f5f7f11d50a3a\Microsoft.mshtml.dll

b) Download DLL from net and put into "\...\obj\Debug\Interop.SHDocVw.dll" folder

2) Create one cs file “Browser.cs” in same project
using SHDocVw;
public class Browser
{
public static IWebBrowser2 TheInstance = new InternetExplorerClass() as IWebBrowser2;
}
3) Add this on the top of the page
using mshtml;
write following code in the onload event or Startup event.
object missing = System.Reflection.Missing.Value;
// any URL you can put here like: www.yahoomail.com etc.
Browser.TheInstance.Navigate
ref missing, ref missing, ref missing, ref missing);
Browser.TheInstance.Visible = true;
while (Browser.TheInstance.Busy)
Thread.Sleep(1000);
IHTMLDocument2 doc = Browser.TheInstance.Document as IHTMLDocument2;
IHTMLElement body = doc.body;
IHTMLElementCollection children = body.all as IHTMLElementCollection;
foreach (IHTMLElement child in children)
{
if (child.id != null)
{
string elementName = child.id.Trim();
//if you know in that web page one button control name
//is "btnStartQueue" then you can do click operation from below code
if (elementName.Trim() == "btnStartQueue")
{
child.click();
}
}
}
Ritesh Kumar Kesharwani
Cell : 91-9890301536


Want to start your own business? Learn how on
Yahoo! Small Business.

12 comments:

Santosh Kore said...

thanks dude..... its really helpful.... but I am getting this error whenver I am closing the browser window and agian trying to open it from my windows application. The error is

"The object invoked has disconnected from its clients. (Exception from HRESULT: 0x80010108 (RPC_E_DISCONNECTED))"

It would be appreciable if you could help me out of this..

Solitaire said...

hi dude,i want to open the particular url multiple times.
But it is just refreshing the same window.For example www.yahoo.com
I have put the code into loop stating 5 times the window should
open.
If you can help,please do it.

Unknown said...

create instance of the browser class, and dont use static keyword

PoojaAmit said...

Thaks..Your code is really helpful ..but I need to open a url in same window of internet explorer
every time.
How can it be done.
If you could hepl me out pleae do so..
Thanks in Advance

rajesh said...

Thanks for posting this helpful script.
I need to call a button click event after calling webpage.
I have tried in the way as you have described but on local I am not able to debug the button click event.

Unknown said...

thanks .........its really helpful link....
but i want to create Dynamic table data report on .aspx page if you have any idea to create report than please help me
thanks in advance

eman.ref3at said...

Thank you very much ,but i am getting this error---> "COM object with CLSID '{0002DF01-0000-0000-C000-000000000046}' cannot be created due to the following error: Class not registered ."

It would be appreciable ,if you help me in this error

Ritesh_Kesharwani said...

Hi Sanju,

you can open perticular url multiple time but you have to give unique window name for all your new window which you are opening from C# window application.

Anonymous said...

vab банк
vab банк
[url=http://globalist.org.ua/?p=19244]vab банк[/url]
http://globalist.org.ua/?p=19244 - vab банк

Nitin Singh Chaurasia said...

hi,
is there any other way to do the same thing without using Microsoft.mshtml.dll and Interop.SHDocVw.dll DLLs ? i m asking this coz while creating setup for my windows application Interop.SHDocVw.dll giving error: "Interop.SHDocVw.dll should be excluded". this is coming coz this dll is the part of windows\system32. plz help me and provide the solution if possible.

Nitin Singh Chaurasia said...

hi,
is there any other way to do the same thing without using Microsoft.mshtml.dll and Interop.SHDocVw.dll DLLs ? i m asking this coz while creating setup for my windows application Interop.SHDocVw.dll giving error: "Interop.SHDocVw.dll should be excluded". this is coming coz this dll is the part of windows\system32. plz help me and provide the solution if possible ASAP.

Nitin Singh Chaurasia said...

hi,
i got the solution of my problem. anyways thanks.....