這些天因為工作需要,要将一些html檔案轉換為chm檔案,當然是需要和程式結合在一起。
後來找到NDoc,裡頭有一段代碼是相關的,于是開始分析代碼,寫完之後,
總結:主要是利用微軟的hhc.exe來編譯html檔案,程式需要将具體的資料寫入hhp和hhc檔案。
主要代碼如下:
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("[INFOTYPES]");
streamHtmlHelp.Close();
轉自:http://www.cnblogs.com/monthkey/archive/2004/06/15/15995.aspx