用C#實現將HTML檔案轉換為CHM檔案)

來源:互聯網
上載者:User

這些天因為工作需要,要將一些html檔案轉換為chm檔案,當然是需要和程式結合在一起。
後來找到NDoc,裡頭有一段代碼是相關的,於是開始分析代碼,寫完之後,總結:主要是利用微軟的hhc.exe來編譯html檔案,程式需要將具體的資料寫入hhp和hhc檔案。
主要代碼如下:
複製C#代碼儲存代碼public void CompileProject()
{
    Process helpCompileProcess = new Process();  //建立新的進程,NDOC採用Process啟動HHC.EXE來Compile一個CHM檔案

    try
    {
        ////判斷檔案是否存在並不被佔用
        try
        {
            string path = _chmFile;  //chm產生路徑

            if (File.Exists(path))
            {
                File.Delete(path);
            }
        }
        catch
        {
            throw new Exception("檔案被開啟!");
        }

        ProcessStartInfo processStartInfo = new ProcessStartInfo();
        processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        processStartInfo.FileName = hhcFile;  //調入HHC.EXE檔案 
        processStartInfo.Arguments = "\"" + Path.GetFullPath(GetPathToProjectFile()) + "\"";//擷取空的HHP檔案

        helpCompileProcess.StartInfo = processStartInfo;

        //開始產生....
        helpCompileProcess.Start();

        helpCompileProcess.WaitForExit(); //組件無限期地等待關聯進程退出

        if (helpCompileProcess.ExitCode == 0)
        {
            MessageBox.Show(new Exception().Message);
        }

    }
    finally
    {
        helpCompileProcess.Close();
    }
}
public void OpenProjectFile()
{
    FileStream fs = new FileStream(GetPathToProjectFile(), FileMode.Create);
    streamHtmlHelp = new System.IO.StreamWriter(fs, System.Text.Encoding.GetEncoding("GB18030"));
    streamHtmlHelp.WriteLine("[FILES]");
}
public void AddFileToProject(string filename)
{
    streamHtmlHelp.WriteLine(filename);
}
public void CloseProjectFile(string title)
{
    streamHtmlHelp.WriteLine();
    streamHtmlHelp.WriteLine("[OPTIONS]");
    streamHtmlHelp.WriteLine("Title=" + title);
    streamHtmlHelp.WriteLine("Compatibility=1.1 or later");
    streamHtmlHelp.WriteLine("Compiled file=" + GetCompiledHtmlFilename());  //chm檔案名稱
    streamHtmlHelp.WriteLine("Contents file=" + GetContentsHtmlFilename());  //hhc檔案名稱
    streamHtmlHelp.WriteLine("Default topic=" + _defaultTopic);  //預設頁
    streamHtmlHelp.WriteLine("Display compile progress=No"); //是否顯示編譯過程
    streamHtmlHelp.WriteLine("Language=0x804 中文(中國)");  //chm檔案語言

    streamHtmlHelp.WriteLine();
    streamHtmlHelp.WriteLine("[INFOTYPES]");

    streamHtmlHelp.Close();
}
轉自:http://www.cnblogs.com/monthkey/archive/2004/06/15/15995.aspx

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.