Search code examples
c#embedded-resourcechm

Open CHM File from Resources c#


I have a CHM file and a help menu and I want to add that file to resources but when I added it to resources it does not work. I tried to add to subfolder in Resources but still no use

void HelpToolStripMenuItemClick(object sender, EventArgs e)
        {
        string filePath = Directory.GetCurrentDirectory() + "\\Help\\abc.chm";

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

    }

    Help.ShowHelp(this, filePath);

}

I want to work even when this to work even when the setup is installed on any computer

I would be better if any tells how to embedded in my setup


Solution

  • First of all, add the help file to your project and open the Properties window for that file. In the CopyToOutputDirectory, choose ‘Copy always’ or ‘Copy if newer’.

    This will make sure, that when you’re debugging/testing your application, that it will copy the file to the bin folder.

    Start your project in Debug mode and check your bin/debug output folder. Do the same for Release mode and output folder. The CHM should reside there and gets included in your deployment.

    A sample code snippet for calling CHM's:

    private const string sHTMLHelpFileName = "CHM-example.chm";
    ...
    
    private void button1_Click(object sender, EventArgs e) {
      System.Windows.Forms.Help.ShowHelp(this, Application.StartupPath + @"\" + sHTMLHelpFileName);
      }
    

    For download I provide a C# VS2008 Project including the code above and the help files with different help viewer windows (different CHM files for show case only).