I want my application to be able to access an external help file. I also want the user to be able to install the program is his chosen folder. The problem this creates is that I don't know how to specify the Namespace for the help provider. I want to have the help file installed in the "Help" folder under Windows. I've been trying to figure this out, and can't. Can someone help me out here? Here's the code I was trying to use for the click event:

// get environment variable representing the Windows folder
String s = Environment.GetEnvironmentVariable("windir");
// set h to WindowsPath\Help\helpfile
String h = s + "/Help/MyHelpFile.chm";
Help.ShowHelp(this, HelpProvider1.HelpNamespace);

When I run this, I get no build errors, but when I click the Help menu item, it throws an exception indicating the URL isn't correct. I have tried several ways to make the Namespace property be the path to the .chm file with no success.

Dani AI

Generated

Brief recap: wanted to ship a CHM and let the user pick the install folder; the runtime error turned out to be an invalid path string. and were right to point out the escaping/parameter issue, but a small, more robust pattern avoids fragile string concatenation and prevents the same problem when the app is installed in different locations.

Prefer building the path with the framework, confirm the file exists, and pass a well‑formed file URL to the help APIs. Example pattern:

using System.IO;

string helpPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Help", "MyHelpFile.chm");
if (!File.Exists(helpPath))
    helpPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "Help", "MyHelpFile.chm");

if (File.Exists(helpPath))
{
    helpProvider1.HelpNamespace = helpPath;
    Help.ShowHelp(this, new Uri(helpPath).AbsoluteUri);
}
else
{
    // fallback behavior: log, show a message, or look in other install locations
}

Notes and troubleshooting tips:

  • Path.Combine + Environment.GetFolderPath avoids separator mistakes and is more portable than manual string concatenation.
  • Setting helpProvider1.HelpNamespace at runtime makes the component location‑aware; verify File.Exists(helpPath) before calling ShowHelp to avoid exceptions.
  • Avoid writing to the Windows folder when possible (modern Windows may require elevation). Prefer installing help alongside the app or in a common data folder.
  • CHM files can be blocked if downloaded or opened from network locations; if a CHM fails to render, check its file properties (Unblock) or test from a local path.
  • For context help set per-control keywords/navigators, for example:
    helpProvider1.SetHelpNavigator(textBox1, HelpNavigator.Topic);
    helpProvider1.SetHelpKeyword(textBox1, "gettingStarted.htm");

The original runtime error was simply a bad path; the above pattern makes the path resolution explicit and safer for real installs.

Recommended Answers

All 7 Replies

String h = s + "/Help/MyHelpFile.chm";

shouldn't that be:

String h = s + @"\Help\MyHelpFile.chm";

?

I tried that, but I must still be doing something wrong. Seems to me the key is in the line:

Help.ShowHelp(this, HelpProvider1.HelpNamespace);

...but I can't find anything in my quite limited understanding to make it work. I know that the Namespace property points to the file and it's path, but I can't figure out how to give the namespace property anything other than an explicit filename.

try this

String s = Environment.GetEnvironmentVariable("windir");
			// set h to WindowsPath\Help\helpfile
			String h = s + "\\Help\\MyHelpFile.chm";
			
			Help.ShowHelp(this, h);

your second parameter for ShowHelp is wrong (it has to be the path to your help file)

try this:

/ get environment variable representing the Windows folder
String s = Environment.GetEnvironmentVariable("windir");
// set h to WindowsPath\Help\helpfile
String h = s + @"\Help\MyHelpFile.chm";
Help.ShowHelp(this, h);

um, h is the path for the helpfile....

um, h is the path for the helpfile....

yes, didn't notice your previous post ;)

Thanks, gentlemen!
You know what? I feel pretty silly for not trying that. I actually thought about it, but I kept telling myself I couldn't directly use a variable for that parameter, even though it made sense.
I guess I am developing some programming instinct but just haven't learned to trust it yet.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.