天天看點

C#生成CHM檔案

HTML Help Workshop介紹:微軟出品的HTML Help WorkShop制作chm檔案的最佳工具。

(下載下傳位址:http://www.microsoft.com/en-us/download/details.aspx?id=21138)

(HTML HELP WORKSHOP教:http://wenku.baidu.com/view/a90adbd249649b6648d74794.html)

本文,我們将用程式設計的方法來實作将html檔案編譯成CHM檔案。在開始程式設計之前,我們有必要了解下HTML Help Workshop是怎麼生成CHM的。

HTML Help Workshop編譯成CHM檔案需要如下三個檔案,分别以hhp,hhc,hhk為檔案字尾名。

hhp:CHM工程檔案,CHM目标檔案屬性95%的參數都在這裡被确定.

hhc,清單檔案,确定目标檔案中左側樹形清單中"目錄"頁籤下的内容.

hhk,索引檔案,确定目标檔案中左側樹形清單中"索引"頁籤下的内容.

hhp幾乎就是一個标準的ini檔案.分為三個小節Option,Windows,Files.

典型的配置檔案(test.hhp)結構如下:

[OPTIONS]
Title= test
Compatibility=1.1 or later
Default Window=Main
Default topic=index.html
Display compile progress=No
Language=0x804 中文(中國)

[WINDOWS]
Main=test","test.hhc","test.hhk","index.html","index.html",,,,,0x20,180,0x104E,[80,60,720,540],0x0,0x0,,,,,0

[FILES]
           

内容頁項目資源(test.hhc)檔案結構如下:

DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<HTML>
<HEAD>
<meta name="GENERATOR" content="Microsoft? HTML Help Workshop 4.1">

HEAD>
<BODY>
<OBJECT type="text/site properties">
<param name="Window Styles" value="0x237">
OBJECT>
<UL>
<LI> <OBJECT type="text/sitemap">
<param name="Name" value="NewTopic">
OBJECT>
<UL>
<LI> <OBJECT type="text/sitemap">
<param name="Name" value="NewTopic">
<param name="Local" value="NewTopic.html">
OBJECT>
UL>
UL>
BODY>
HTML>
           

引檔案(test.hhk))也是一個HTML檔案,它包含若幹個關鍵詞,當使用者打開chm檔案後,單擊索引标簽并輸入一個關鍵詞後,chm檔案将顯示與這個關鍵詞有關的主題的清單,使大家非常友善地找到相關主題。 典型的檔案結構如下:

DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<HTML>
<HEAD>
<meta name="GENERATOR" content="Microsoft? HTML Help Workshop 4.1">
 
HEAD>
<BODY>
<UL>
 <LI> <OBJECT type="text/sitemap">
 <param name="Name" value="NewTopic">
 <param name="Local" value="NewTopic.html">
 OBJECT>
UL>
BODY>
HTML>
           

将線上網頁儲存為chm檔案 代碼如下:

protected void Page_Load(object sender, EventArgs e)
    {
        CreateIndexHtml();  
        CreateCHM(@"D:\Program Files (x86)\HTML Help Workshop\hhc.exe");//生成CHM檔案
      
    }
    /// <summary>
    /// 将百度的首頁下載下傳到本地
    /// </summary>
    private void CreateIndexHtml() {
        string url = "http://www.baidu.com";
        HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
        HttpWebResponse myResp = (HttpWebResponse)myReq.GetResponse();
        StreamReader respStream = new StreamReader(myResp.GetResponseStream(), Encoding.Default);
        string respStr = respStream.ReadToEnd();
        respStream.Close();
        FileStream fs = new FileStream(Request.PhysicalApplicationPath + @"\index.html", FileMode.Create, FileAccess.Write);
        StreamWriter sw = new StreamWriter(fs, Encoding.Default);
        sw.Write(respStr);
        sw.Close();
    
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="hhcFileUrl">HTML Help Workshop安裝後hhc.exe檔案的路徑</param>
    /// <returns></returns>
    private bool CreateCHM(string hhcFileUrl)
    {
        Process helpCompileProcess = new Process(); //建立新的程序,用Process啟動HHC.EXE來Compile一個CHM檔案
        try
        {
            ProcessStartInfo processStartInfo = new ProcessStartInfo();
            processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            processStartInfo.FileName = hhcFileUrl; //調入HHC.EXE檔案 
            processStartInfo.Arguments = "\"" + Path.GetFullPath(Request.PhysicalApplicationPath + @"\test.hhp") + "\"";//擷取先前做好的HHP(裡面會引用hhc.hhk等檔案)檔案
            processStartInfo.UseShellExecute = false;
            helpCompileProcess.StartInfo = processStartInfo;
            helpCompileProcess.Start();
            helpCompileProcess.WaitForExit(); //元件無限期地等待關聯程序退出

            if (helpCompileProcess.ExitCode == 0)
            {
                Response.Write(new Exception().Message);
                return false;
            }
        }
        finally
        {
            helpCompileProcess.Close();
        }
        return true;
    }
           
C#生成CHM檔案