首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 取得 IE 瀏覽器當前網頁 HTML 文件 (二):主控台程式完整代碼

     編輯:關於C語言

完整可執行代碼


using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using SHDocVw;
using mshtml;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            ShellWindows shellWindows = new ShellWindowsClass();
            foreach (InternetExplorer ie in shellWindows)
            {
                string fileName = Path.GetFileNameWithoutExtension(ie.FullName).ToLower();
                if (fileName.Equals("iexplore"))
                {
                    Console.WriteLine("Web Site: {0}", ie.LocationURL);
                    IHTMLDocument2 htmlDoc = ie.Document as IHTMLDocument2;
                    string snippet = (htmlDoc != null) ? htmlDoc.body.outerHtml.Substring(0, 40) : "***Failed***";
                    Console.WriteLine("Document Snippet: {0}", snippet);
                    Console.WriteLine();
                }
            }
        }
    }
}

必要 COM 參考

  1. Microsoft Internet Controls (SHDocVw.dll) → 提供 ShellWindows、InternetExplorer。
  2. Microsoft HTML Object Library (mshtml.dll) → 提供 IHTMLDocument2。

專案需勾選「Embed Interop Types」或部署時包含 PIA。程式列舉所有殼層視窗,篩選 iexplore 進程,輸出網址與 HTML 片段。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved