zoukankan      html  css  js  c++  java
  • 用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();
    }

  • 相关阅读:
    OpenCV 机器学习之 支持向量机的使用方法实例
    Lua中调用C函数(lua-5.2.3)
    我的Hook学习笔记
    几种开源分词工具的比較
    利用JasperReport+iReport进行Web报表开发
    移动前端开发者必知必会:移动设备概述
    图表插件--jqplot交互演示样例
    算法之二叉树各种遍历
    repo的小结
    sqlite3经常使用命令&语法
  • 原文地址:https://www.cnblogs.com/liufei88866/p/1914748.html
Copyright © 2011-2022 走看看