用C#实现将HTML文件转换为CHM文件(转)

这些天因为工作需要,要将一些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();
}

原文地址:https://www.cnblogs.com/liufei88866/p/1984575.html