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;
//HHP、HHC、HHK文件保存路径
private string SaveHHPFilePath;
private string SaveHHCFilePath;
private string SaveHHKFilePath;
//其中FilePath为Html文件所以在路径,SaveChmFileName生成CHM文件的名称
public HtmlToChmClass(string FilePath, string SaveChmFileName)
{
SearchFilePath = FilePath;
SaveHHPFilePath = SearchFilePath + """Result.hhp";
SaveHHCFilePath = SearchFilePath + """Result.hhc";
SaveHHKFilePath = SearchFilePath + """Result.hhk";
//生成HHP、HHC、HHK文件
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® 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® 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工程HHCExeFilePath为hhc.exe文件的目录HHPFilePath为生成的HHP文件目录
private void CompileChmProject(string HHCExeFilePath, string HHPFilePath)
{
//创建新的进程,采用Process启动HHC.EXE来Compile一个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文件SaveChmFileName为CHM的文件名称
public void RunChmFile(string SaveChmFileName)
{
//创建新的进程,采用Process启动HHC.EXE来Compile一个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();
}
}
}