将HTML转换成CHM帮助文档

using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

using System.Diagnostics;

 

public class HtmlToChmClass

{

    //保存上一级目录

    private static string OldFileDir = "";

    //UL计数器

    private static int ULCount = 1;

    //HTM文件路径

    private string SearchFilePath;

    //HHPHHCHHK文件保存路径

    private string SaveHHPFilePath;

    private string SaveHHCFilePath;

    private string SaveHHKFilePath;

    //其中FilePathHtml文件所以在路径,SaveChmFileName生成CHM文件的名称

    public HtmlToChmClass(string FilePath, string SaveChmFileName)

    {

        SearchFilePath = FilePath;

        SaveHHPFilePath = SearchFilePath + """Result.hhp";

        SaveHHCFilePath = SearchFilePath + """Result.hhc";

        SaveHHKFilePath = SearchFilePath + """Result.hhk";

        //生成HHPHHCHHK文件

        GetChmHHPFile(SaveChmFileName);

        GetChmHHCFile();

        GetChmHHKFile();

        //编译CHM工程

        CompileChmProject(FilePath, FilePath);

    }

 

 

    //生成HHP文件

    private void GetChmHHPFile(string SaveChmFileName)

    {

        //如果HHP文件已经存在则删除

        if (File.Exists(SaveHHPFilePath))

        {

            File.Delete(SaveHHPFilePath);

        }

        WriteChmHHPFileHead(SaveChmFileName);

        DealWithChmHHPFile(SearchFilePath);

        WriteChmHHPFileTail();

    }

 

    private void DealWithChmHHPFile(string strFromPath)

    {

        //创建数组保存源文件夹下的文件名

        string[] strFiles = Directory.GetFiles(strFromPath, "*.htm");

        //循环搜索文件

        for (int i = 0; i < strFiles.Length; i++)

        {

            //取得拷贝的文件名,只取文件名,地址截掉。

            int Length = SearchFilePath.Length + 1;

            string FilePath = strFiles[i].Substring(Length, strFiles[i].Length - Length);

            WriteChmHHPFileBody(FilePath);

        }

 

        //创建DirectoryInfo实例

        DirectoryInfo dirInfo = new DirectoryInfo(strFromPath);

        //取得源文件夹下的所有子文件夹名称

        DirectoryInfo[] ZiPath = dirInfo.GetDirectories();

        for (int j = 0; j < ZiPath.Length; j++)

        {

            //获取所有子文件夹名

            string strZiPath = strFromPath + """" + ZiPath[j].ToString();

            //把得到的子文件夹当成新的源文件夹,从头开始新一轮的搜索

            DealWithChmHHPFile(strZiPath);

        }

    }

 

    private void WriteChmHHPFileHead(string SaveChmFileName)

    {

        StreamWriter FileWrite = new StreamWriter(SaveHHPFilePath, true, Encoding.Default);

        FileWrite.WriteLine("[OPTIONS]");

        FileWrite.WriteLine("Compatibility=1.1 or later");

        FileWrite.WriteLine("Compiled file={0}", SaveChmFileName);

        FileWrite.WriteLine("Contents file=Result.hhc");

        FileWrite.WriteLine("Index file=Result.hhk");

        FileWrite.WriteLine("Title=帮助文档");

        FileWrite.WriteLine("Full-text search=Yes");

        FileWrite.WriteLine("Language=0x804 中文(中国)");

        FileWrite.WriteLine();

        FileWrite.WriteLine("[FILES]");

        FileWrite.Close();

    }

 

    private void WriteChmHHPFileTail()

    {

        StreamWriter FileWrite = new StreamWriter(SaveHHPFilePath, true, Encoding.Default);

        FileWrite.WriteLine();

        FileWrite.WriteLine("[INFOTYPES]");

        FileWrite.Close();

    }

 

    private void WriteChmHHPFileBody(string FilePath)

    {

        StreamWriter FileWrite = new StreamWriter(SaveHHPFilePath, true, Encoding.Default);

        FileWrite.WriteLine(FilePath);

        FileWrite.Close();

    }

 

    //生成HHC文件

    private void GetChmHHCFile()

    {

        //如果HHC文件已经存在则删除

        if (File.Exists(SaveHHCFilePath))

        {

            File.Delete(SaveHHCFilePath);

        }

        WriteChmHHCFileHead();

        DealWithChmHHCFile(SearchFilePath);

        WriteChmHHCFileTail();

    }

 

    private void DealWithChmHHCFile(string strFromPath)

    {

        //创建数组保存源文件夹下的文件名

        string[] strFiles = Directory.GetFiles(strFromPath, "*.htm");

        //循环搜索文件

        for (int i = 0; i < strFiles.Length; i++)

        {

            //取得拷贝的文件名,只取文件名,地址截掉。

            int Length = SearchFilePath.Length + 1;

            string FilePath = strFiles[i].Substring(Length);

            WriteChmHHCFileBody(FilePath);

        }

        //创建DirectoryInfo实例

        DirectoryInfo dirInfo = new DirectoryInfo(strFromPath);

        //取得源文件夹下的所有子文件夹名称

        DirectoryInfo[] ZiPath = dirInfo.GetDirectories();

        for (int j = 0; j < ZiPath.Length; j++)

        {

            //获取所有子文件夹名

            string strZiPath = strFromPath + """" + ZiPath[j].ToString();

            //把得到的子文件夹当成新的源文件夹,从头开始新一轮的搜索

            DealWithChmHHCFile(strZiPath);

        }

    }

 

    private void WriteChmHHCFileHead()

    {

        StreamWriter FileWrite = new StreamWriter(SaveHHCFilePath, true, Encoding.Default);

        FileWrite.WriteLine("<!DOCTYPE HTML PUBLIC ""-//IETF//DTD HTML//EN"">");

        FileWrite.WriteLine("<HTML>");

        FileWrite.WriteLine("<HEAD>");

        FileWrite.WriteLine("<meta name=""GENERATOR"" content=""Microsoft&reg; HTML Help Workshop 4.1"">");

        FileWrite.WriteLine("<!-- Sitemap 1.0 -->");

        FileWrite.WriteLine("</HEAD><BODY>");

        FileWrite.WriteLine();

        FileWrite.WriteLine("<UL>");

        FileWrite.Close();

    }

 

    private void WriteChmHHCFileTail()

    {

        StreamWriter FileWrite = new StreamWriter(SaveHHCFilePath, true, Encoding.Default);

        for (int i = 0; i < ULCount; i++)

        {

            FileWrite.WriteLine("</UL>");

        }

        FileWrite.WriteLine("</BODY></HTML>");

        FileWrite.Close();

    }

 

 

    private void WriteChmHHCFileBody(string NewFileDir)

    {

        string tempNewFileDir;

        if (NewFileDir.LastIndexOf('""') != -1)

        {

            int Length = NewFileDir.LastIndexOf('""');

            tempNewFileDir = NewFileDir.Substring(0, Length);

        }

        else

        {

            tempNewFileDir = "";

        }

 

        WriteChmHHCFileUL(tempNewFileDir);

        StreamWriter FileWrite = new StreamWriter(SaveHHCFilePath, true, Encoding.Default);

        string FileName = NewFileDir.Substring(NewFileDir.LastIndexOf('""') + 1, NewFileDir.Length - NewFileDir.LastIndexOf('""') - 5);

        FileWrite.WriteLine("     <LI><OBJECT type=""text/sitemap"">");

        FileWrite.WriteLine("     <param name=""Name"" value=""{0}"">", FileName);

        FileWrite.WriteLine("     <param name=""Local"" value=""{0}"">", NewFileDir);

        FileWrite.WriteLine("     <param name=""ImageNumber"" value=""11"">");

        FileWrite.WriteLine("     </OBJECT>");

        FileWrite.Close();

    }

 

    private void WriteChmHHCFileUL(string NewFileDir)

    {

        StreamWriter FileWrite = new StreamWriter(SaveHHCFilePath, true, Encoding.Default);

        string[] NewFileSplit = NewFileDir.Split(new Char[] { '""' });

        string[] OldFileSplit = OldFileDir.Split(new Char[] { '""' });

        int NewFileLength = NewFileSplit.Length;

        int OldFileLength = OldFileSplit.Length;

        int nCount = (NewFileLength > OldFileLength ? OldFileLength : NewFileLength);

        int index = 0;

        for (index = 0; index < nCount; index++)

        {

            if (NewFileSplit[index].ToString() != OldFileSplit[index].ToString())

            {

                break;

            }

        }

        if (NewFileDir.IndexOf(OldFileDir) != -1)

        {

            for (int begin = index; begin < NewFileLength; begin++)

            {

                ULCount++;

                FileWrite.WriteLine("     <LI><OBJECT type=""text/sitemap"">");

                FileWrite.WriteLine("     <param name=""Name"" value=""{0}"">", NewFileSplit[begin]);

                FileWrite.WriteLine("     <param name=""Local"" value="""">");

                FileWrite.WriteLine("     </OBJECT>");

                FileWrite.WriteLine("<UL>");

            }

        }

        else

        {

            for (int begin = OldFileLength - 1; begin >= index; begin--)

            {

                ULCount--;

                FileWrite.WriteLine("</UL>");

            }

 

            for (int begin = index; begin < NewFileLength; begin++)

            {

                ULCount++;

                FileWrite.WriteLine("     <LI><OBJECT type=""text/sitemap"">");

                FileWrite.WriteLine("     <param name=""Name"" value=""{0}"">", NewFileSplit[begin]);

                FileWrite.WriteLine("     <param name=""Local"" value="""">");

                FileWrite.WriteLine("     </OBJECT>");

                FileWrite.WriteLine("<UL>");

            }

        }

 

        FileWrite.Close();

        OldFileDir = NewFileDir;

    }

 

 

    //生成HHK文件

    private void GetChmHHKFile()

    {

        //如果HHK文件已经存在则删除

        if (File.Exists(SaveHHKFilePath))

        {

            File.Delete(SaveHHKFilePath);

        }

 

        WriteChmHHKFileHead();

        DealWithChmHHKFile(SearchFilePath);

        WriteChmHHKFileTail();

    }

 

 

    private void DealWithChmHHKFile(string strFromPath)

    {

        //创建数组保存源文件夹下的文件名

        string[] strFiles = Directory.GetFiles(strFromPath, "*.htm");

        //循环搜索文件

        for (int i = 0; i < strFiles.Length; i++)

        {

            //取得拷贝的文件名,只取文件名,地址截掉。

            int Length = SearchFilePath.Length + 1;

            string FilePath = strFiles[i].Substring(Length);

            WriteChmHHKFileBody(FilePath);

        }

 

        //创建DirectoryInfo实例

        DirectoryInfo dirInfo = new DirectoryInfo(strFromPath);

        //取得源文件夹下的所有子文件夹名称

        DirectoryInfo[] ZiPath = dirInfo.GetDirectories();

        for (int j = 0; j < ZiPath.Length; j++)

        {

            //获取所有子文件夹名

            string strZiPath = strFromPath + """" + ZiPath[j].ToString();

            //把得到的子文件夹当成新的源文件夹,从头开始新一轮的搜索

            DealWithChmHHKFile(strZiPath);

        }

 

 

    }

 

 

    private void WriteChmHHKFileHead()

    {

        StreamWriter FileWrite = new StreamWriter(SaveHHKFilePath, true, Encoding.Default);

        FileWrite.WriteLine("<!DOCTYPE HTML PUBLIC ""-//IETF//DTD HTML//EN"">");

        FileWrite.WriteLine("<HTML>");

        FileWrite.WriteLine("<HEAD>");

        FileWrite.WriteLine("<meta name=""GENERATOR"" content=""Microsoft&reg; HTML Help Workshop 4.1"">");

        FileWrite.WriteLine("<!-- Sitemap 1.0 -->");

        FileWrite.WriteLine("</HEAD><BODY>");

        FileWrite.WriteLine();

        FileWrite.WriteLine("<UL>");

        FileWrite.Close();

    }

 

 

    private void WriteChmHHKFileTail()

    {

        StreamWriter FileWrite = new StreamWriter(SaveHHKFilePath, true, Encoding.Default);

        FileWrite.WriteLine("</UL>");

        FileWrite.WriteLine("</BODY></HTML>");

        FileWrite.Close();

    }

 

 

    private void WriteChmHHKFileBody(string NewFileDir)

    {

        StreamWriter FileWrite = new StreamWriter(SaveHHKFilePath, true, Encoding.Default);

        string FileName = NewFileDir.Substring(NewFileDir.LastIndexOf('""') + 1, NewFileDir.Length - NewFileDir.LastIndexOf('""') - 5);

        FileWrite.WriteLine("     <LI><OBJECT type=""text/sitemap"">");

        FileWrite.WriteLine("     <param name=""Name"" value=""{0}"">", FileName);

        FileWrite.WriteLine("     <param name=""Local"" value=""{0}"">", NewFileDir);

        FileWrite.WriteLine("     <param name=""ImageNumber"" value=""11"">");

        FileWrite.WriteLine("     </OBJECT>");

        FileWrite.Close();

    }

 

 

    //编译CHM工程HHCExeFilePathhhc.exe文件的目录HHPFilePath为生成的HHP文件目录

    private void CompileChmProject(string HHCExeFilePath, string HHPFilePath)

    {

        //创建新的进程,采用Process启动HHC.EXECompile一个CHM文件

        Process helpCompileProcess = new Process();

        try

        {

            ProcessStartInfo processStartInfo = new ProcessStartInfo();

            processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;

            processStartInfo.FileName = "C:/Program Files/HTML Help Workshop/hhc.exe";

            processStartInfo.Arguments = HHPFilePath + """Result.hhp";

            helpCompileProcess.StartInfo = processStartInfo;

            //开始编译CHM文件

            helpCompileProcess.Start();

            //编译直到HHC.exe进程退出

            helpCompileProcess.WaitForExit();

        }

        finally

        {

            helpCompileProcess.Close();

        }

    }

 

 

    //运行生成的CHM文件SaveChmFileNameCHM的文件名称

    public void RunChmFile(string SaveChmFileName)

    {

        //创建新的进程,采用Process启动HHC.EXECompile一个CHM文件

        Process helpCompileProcess = new Process();

        try

        {

            ProcessStartInfo processStartInfo = new ProcessStartInfo();

            processStartInfo.WindowStyle = ProcessWindowStyle.Normal;

            processStartInfo.FileName = SearchFilePath + """" + SaveChmFileName;

            helpCompileProcess.StartInfo = processStartInfo;

            //运行CHM文件

            helpCompileProcess.Start();

        }

        finally

        {

            helpCompileProcess.Close();

        }

    }

}

转载于:https://www.cnblogs.com/jimtomjim/archive/2009/08/21/1551545.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值