【IT168 技术文档】在上篇《手把手教你C#生成CHM文件》中,我们利用微软自带的hhc.exe以编程的方式创建一个CHM文件,而且调用的是一个静态的HMTL文件。
在中篇中,实现以下几个目标
1.将在线的网页保存为CHM文件
2.我们将对我们进行编译的CHM文件进行反编译,使用的还是微软自带的一个exe(hh.exe)。
3.以编程的方式将CHM文件转换为Word
在中篇中,把界面稍微调整了下,如下图
一、将在线的网页保存为CHM文件
曾尝试直接使用网址来编译html文件,结果一直报错,于是就放弃了。现在实现的方法的思想是这样的:先将输入的url地址的网页保存到本地,然后利用上一篇中的方法生成CHM文件。不过经测试,这样的效率还是比较低的,主要的花费在将htm文件下载到本地,如果带宽不够的话,将会很慢,不过总归是种方法,大家如果有更好的解决方案,希望能告诉我。
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse myResp = (HttpWebResponse)myReq.GetResponse();
StreamReader respStream = new StreamReader(myResp.GetResponseStream(), Encoding.Default);
string respStr = respStream.ReadToEnd(); respStream.Close();
FileStream fs = new FileStream(startPath+@"\test.htm", FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs, Encoding.Default);
sw.Write(respStr);
sw.Close();
HttpWebResponse myResp = (HttpWebResponse)myReq.GetResponse();
StreamReader respStream = new StreamReader(myResp.GetResponseStream(), Encoding.Default);
string respStr = respStream.ReadToEnd(); respStream.Close();
FileStream fs = new FileStream(startPath+@"\test.htm", FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs, Encoding.Default);
sw.Write(respStr);
sw.Close();
思路是将网页保存在本地的,startPath为项目所在路径。 大家可以以http://www.baidu.com/index.htm为例(注意要以htm或html为结尾),测试下,看看能不能正常将百度的首页保存到CHM文件中。效果图是这样的:
二、反编译CHM
反编译CHM的方法同初篇中的利用Process类来进行。
/// <summary>
/// 反编译CHM文件
/// </summary>
/// <param name="CHMFile">CHM文件名</param>
/// <returns>返回hhc文件名</returns>
/// <remarks>uses the <see cref="DecompileChm"></see></remarks>
public string DecompileChm(string CHMFile)
{
string pathDir = Path.GetDirectoryName(CHMFile);//得到chm文件的绝对路径
pathDir = Path.Combine(pathDir, Path.GetFileNameWithoutExtension(CHMFile));
return DecompileChm(CHMFile, ref pathDir);
}
/// <summary>
/// 反编译CHM文件
/// </summary>
/// <param name="CHMFile">CHM文件名</param>
/// <param name="FolderToPut">反编译后的文件存放路径</param>
/// <returns>返回反编译后的hhc文件名</returns>
/// <remarks>使用hh.exe反编译</remarks>
public string DecompileChm(string CHMFile, ref string FolderToPut)
{
if ((!System.IO.File.Exists(CHMFile)))
{
throw new ArgumentException(CHMFile+"文件不存在");
}
if ((!Directory.Exists(FolderToPut)))
{
FolderToPut = FolderToPut.Replace(" ", "_");
Directory.CreateDirectory(FolderToPut);
}
DirectoryInfo di = new DirectoryInfo(FolderToPut);
if ((di.Name.Contains(" ")))
{
throw new ArgumentException("反编译的文件夹名不能包含空格");
}
string strD = null;
strD = " -decompile " + di.FullName + " " + CHMFile;//反编译命令
Console.WriteLine(strD);
Process p = Process.Start("hh.exe", strD);//调用hh.exe进行反编译
p.WaitForExit();
return Directory.GetFiles(FolderToPut, "*.HHC")[0];
}
/// 反编译CHM文件
/// </summary>
/// <param name="CHMFile">CHM文件名</param>
/// <returns>返回hhc文件名</returns>
/// <remarks>uses the <see cref="DecompileChm"></see></remarks>
public string DecompileChm(string CHMFile)
{
string pathDir = Path.GetDirectoryName(CHMFile);//得到chm文件的绝对路径
pathDir = Path.Combine(pathDir, Path.GetFileNameWithoutExtension(CHMFile));
return DecompileChm(CHMFile, ref pathDir);
}
/// <summary>
/// 反编译CHM文件
/// </summary>
/// <param name="CHMFile">CHM文件名</param>
/// <param name="FolderToPut">反编译后的文件存放路径</param>
/// <returns>返回反编译后的hhc文件名</returns>
/// <remarks>使用hh.exe反编译</remarks>
public string DecompileChm(string CHMFile, ref string FolderToPut)
{
if ((!System.IO.File.Exists(CHMFile)))
{
throw new ArgumentException(CHMFile+"文件不存在");
}
if ((!Directory.Exists(FolderToPut)))
{
FolderToPut = FolderToPut.Replace(" ", "_");
Directory.CreateDirectory(FolderToPut);
}
DirectoryInfo di = new DirectoryInfo(FolderToPut);
if ((di.Name.Contains(" ")))
{
throw new ArgumentException("反编译的文件夹名不能包含空格");
}
string strD = null;
strD = " -decompile " + di.FullName + " " + CHMFile;//反编译命令
Console.WriteLine(strD);
Process p = Process.Start("hh.exe", strD);//调用hh.exe进行反编译
p.WaitForExit();
return Directory.GetFiles(FolderToPut, "*.HHC")[0];
}