本篇文章描述如何獲得IE浏覽器當前網頁的Html文檔。
Create a console application in any version of Visual Studio using .Net version 1|2|3|3.5.
Add two Com object references which will allow us to manipulate IE.
用 Visual Studio 的任意版本建立一個控制台程序。
添加2個COM對象引用用來操作IE
Note 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:
添加下面代碼
打開IE獲取Html文檔
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:
程序截圖: