开发者

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.

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. 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.

Curren开发者_运维技巧tly, 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. 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? 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?

Thanks for any help!

Best Regards,

Ian


Apps usually use an installer, or zip archive of some sort. Both methods would allow a user to receive the application and the help files, without having to provide them separately.


Under your project properties - Resources, add a file resource ie:textmag.chm. I use filetype Text for the chm's

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.


Oh, wow. Turns out I didn't need the HHC file as well. 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. I assumed for some reason it needed that in addition to the 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:

data.Count() - 1

I'm not sure why that - 1 was there, the solution I found had it, so I just left it there. 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. All is well. Thanks for the suggestions, everyone!


So a solution is:
1) Copy your chm file to the required project folder
2) In your Visual C# solution explorer add existing item to the project (your chm file).
3) Select the Project menu then project properties.
4) Add existing resource.
5) Add the below code and connect to your help menu item.

   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);
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜