简体   繁体   English

如何在应用程序中嵌入 CHM 文件

[英]How to embed CHM file in application

Hey everyone, I just finished up an application I've been working on for a while now.大家好,我刚刚完成了一个我已经工作了一段时间的应用程序。 Probably the most complex one I've made to date.可能是我迄今为止制作的最复杂的一个。 Due to this, I figured I'd go and make a help document to provide users with some info on it.因此,我想我会 go 并制作帮助文档,为用户提供一些信息。

I've created a CHM file, and set up a helpProvider, however now my problem is how to include this and the HHC (Table of contents) file with my application.我已经创建了一个 CHM 文件,并设置了一个 helpProvider,但是现在我的问题是如何在我的应用程序中包含这个文件和 HHC(目录)文件。 I feel like it'd be a pain to require the user to copy the two files themselves, so I'm trying to store them as embedded resources, then have the application write these out in the current directory.我觉得要求用户自己复制这两个文件会很痛苦,所以我试图将它们存储为嵌入式资源,然后让应用程序将它们写在当前目录中。

Currently, this is the code I'm using:目前,这是我正在使用的代码:

var data = Properties.Resources.RERHelp;
        using (var stream = new FileStream("RERHelp", FileMode.Create))
        {
            stream.Write(data, 0, data.Count() - 1);
            stream.Flush();
        }
        helpProvider1.HelpNamespace = Directory.GetCurrentDirectory() + "\\RERHelp\\RERHelp.chm";

This works just fine, but it means I'd have to run through this twice, once with data set to Properties.Resources.RERHelp, and once for the Table of Contents file.这工作得很好,但这意味着我必须运行两次,一次将数据设置为 Properties.Resources.RERHelp,一次用于目录文件。 Is there a better way to do this?有一个更好的方法吗? Perhaps some way to embed the CHM and HHC files in the application, and access them without writing them to disk?也许某种方式可以在应用程序中嵌入 CHM 和 HHC 文件,并在不将它们写入磁盘的情况下访问它们? If that isn't possible, which I'm thinking it isn't, is there a better way to go about it than how I am currently?如果这是不可能的,我认为不是,有没有比我现在更好的方法来解决 go?

Thanks for any help!谢谢你的帮助!

Best Regards,此致,

Ian伊恩

Under your project properties - Resources, add a file resource ie:textmag.chm.在您的项目属性 - 资源下,添加一个文件资源,即:textmag.chm。 I use filetype Text for the chm's我使用文件类型文本作为 chm

private void HelpToolStripMenuItem_Click(object sender, EventArgs e)
    {
        string helpFileName = "";
        try
        {
            helpFileName = System.IO.Path.Combine(System.Windows.Forms.Application.StartupPath, "Resources") + @"\TextMag.chm";
            Help.ShowHelp(this, helpFileName);

        }
        catch (Exception ex)
        {

            string xxx = ex.Message;
        }

    }

Important: in the properties of the chm file under resources, the Build Action must be Content.重要提示:在资源下chm文件的属性中,Build Action必须是Content。

Apps usually use an installer, or zip archive of some sort.应用程序通常使用安装程序,或某种形式的 zip 存档。 Both methods would allow a user to receive the application and the help files, without having to provide them separately.这两种方法都允许用户接收应用程序帮助文件,而无需单独提供它们。

So a solution is:所以一个解决方案是:
1) Copy your chm file to the required project folder 1)将您的chm文件复制到所需的项目文件夹
2) In your Visual C# solution explorer add existing item to the project (your chm file). 2)在您的 Visual C# 解决方案资源管理器中,将现有项目添加到项目(您的 chm 文件)。
3) Select the Project menu then project properties. 3) Select 项目菜单然后项目属性。
4) Add existing resource. 4) 添加现有资源。
5) Add the below code and connect to your help menu item. 5)添加以下代码并连接到您的帮助菜单项。

   private void WORKING_HELP()
    {
        string filePath = Directory.GetCurrentDirectory() + "\\BlitzHelp.chm";

        try
        {
            //Check if already exists before making
            if (!File.Exists(filePath))
            {
                var data = Properties.Resources.BlitzHelp;
                using (var stream = new FileStream("BlitzHelp.chm", FileMode.Create))
                {
                    stream.Write(data, 0, data.Count());
                    stream.Flush();
                }
                MessageBox.Show("file made");
            }
        }
        catch
        {
            //May already be opened

        }

        Help.ShowHelp(this, filePath);
    }

Oh, wow.哦,哇。 Turns out I didn't need the HHC file as well.原来我也不需要 HHC 文件。 I assumed I did because when I'd open the help dialog, it would say that it couldn't find the table of contents.hhc file.我假设我这样做了,因为当我打开帮助对话框时,它会说它找不到目录.hhc 文件。 I assumed for some reason it needed that in addition to the CHM.我认为出于某种原因,除了 CHM 之外,它还需要它。 I originally just made a method to pass the resources to so as to prevent redundancy, and called that once for the CHM, and once for the HHC, but then I noticed this bit:我最初只是做了一个方法来传递资源以防止冗余,并为 CHM 调用了一次,为 HHC 调用了一次,但后来我注意到了这一点:

data.Count() - 1

I'm not sure why that - 1 was there, the solution I found had it, so I just left it there.我不确定为什么 - 1 在那里,我找到的解决方案有它,所以我把它留在那里。 When I removed that, the program ran, wrote out that file, and could then read it for the help documentation without the complaint of the missing HHC.当我删除它时,程序运行,写出该文件,然后可以阅读它以获取帮助文档,而不会抱怨缺少 HHC。 All is well.一切都很好。 Thanks for the suggestions, everyone!谢谢大家的建议!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM