如何在 C# Winforms 应用程序中显示 chm 文件

How to display chm file in c# winforms application

我已将 .chm 文件添加到我的应用程序根目录。当我使用下面的代码获取文件时,它引用了 bin/release/somehting.chm

的路径
System.Windows.Forms.Help.ShowHelp(this, Application.StartupPath+"\"+"somehting.chm");

我想获取相对于应用程序安装位置的路径。请帮忙

添加到根目录的 chm 文件在部署应用程序后未加载。在 visual studio 中调试时甚至没有加载并且没有给出任何错误。

你需要:

String exeDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);

所以 :

String HelpFilepath = "file://" + Path.Combine(exeDirectory , "somehting.chm");
Help.ShowHelp(this, path);

类似 topic 的回答是:

// get full path to your startup EXE
string exeFile = (new System.Uri(Assembly.GetEntryAssembly().CodeBase)).AbsolutePath;

// get directory of your EXE file
string exeDir = Path.GetDirectoryName(exeFile);

// and open Help
System.Windows.Forms.Help.ShowHelp(this, exeDir+"\"+"somehting.chm");

正如我所看到的,您的问题中调用 Help.ShowHelp 的第一个代码片段还不错。有时我正在使用下面的相关代码。许多解决方案是可能的...... 请注意,打字错误,例如somehting.chm 代码片段令人不安。

private const string sHTMLHelpFileName = "CHM-example.chm";
...

private void button1_Click(object sender, EventArgs e) {
  System.Windows.Forms.Help.ShowHelp(this, Application.StartupPath + @"\" + sHTMLHelpFileName);
  }

因此,请打开 Visual Studio - 解决方案资源管理器并检查您的 CHM 文件的属性。转到下面快照中显示的下拉框并设置 "Always copy"(这里只有德语)。以 Debug 模式启动项目并检查 bin/debug 输出文件夹。对 Release 模式和输出文件夹执行相同操作。 CHM 应该在那里,我希望你的 CHM 调用有效。