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

本文介绍了一种使用NDoc工具将HTML文件批量转换为CHM文件的方法。通过调用微软的hhc.exe实现自动化编译过程,并详细展示了关键代码片段。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值