用C#實現將HTML文件轉換爲CHM文件(轉) - C#探索者 - 博客園

這些天因爲工作需要,要將一些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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章