Use C # to obtain an HTML document for the current web page of IE browser

Source: Internet
Author: User

This article describes how to obtain an HTML document for the current Web page of IE Explorer.

Create a console application in any version of Visual Studio using. Net version 1|2|3|3.5.

ADD two Com object references which would allow us to manipulate IE.

Create a console program with any version of Visual Studio.

Add 2 COM object references to manipulate IE

Note that the code sample below does not require the using directive for the objects, so just add the code as is.

Then find the instances of IE and extract the document:

Add the following code

Open IE get HTML document

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);
mshtml.IHTMLDocument2 htmlDoc
= ie.Document as mshtml.IHTMLDocument2;
Console.WriteLine("  Document Snippet: {0}",
( ( htmlDoc != null ) ? htmlDoc.body.outerHTML.Substring(0, 40)
: "***Failed***" ));
Console.WriteLine("{0}{0}", Environment.NewLine);
}
}

Here is a screen-shot of the output:

Program screenshot:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.