I have a windows forms project. In the current directory I have a Help folder with *.chm files. What is the easiest way to launch them from the application? How can I specify the relative path to them?
-
By 'current' directory do you mean 'working' directory or 'resident' directory? – Grant Thomas Feb 22 '11 at 11:30
-
I suppose, it is working directory. – Peter17 Feb 22 '11 at 11:30
-
try to write path: "Help/"+filename – eba Feb 22 '11 at 11:31
-
It is the name of the file. How can I launch that file? – Peter17 Feb 22 '11 at 11:32
-
if in std program: Process.Start("Help/123.chm"); – eba Feb 22 '11 at 11:34
4 Answers
The Environment.CurrentDirectory property will be set to the location of your .exe file. So if you place your help folder in there, it will be:
// The path to the Help folder.
string directory = Path.Combine(Environment.CurrentDirectory, "Help");
// The path to the Help file.
string filePath = Path.Combine(directory , "myHelpFile.chm");
// Launch the Help file.
Process.Start(filePath);
EDIT: I should say, that Environment.CurrentDirectory
points to the folder where the process started by default in a Windows Forms application, but its value can be changed by some controls (like the OpenFileDialog
- see here for a workaround) during the lifetime of your application. Under a Windows Service, Environment.CurrentDirectory
maps to the %SystemDirectory%
.

- 7,308
- 2
- 35
- 41
-
6`Path.Combine` can take > 2 parameters, so you could just say `Path.Combine(Environment.CurrentDirectory, "Help", "myHelpFile.chm")` – Rich Tebb Feb 22 '11 at 11:36
-
1Environment.CurrentDirectory is not a path to the application – Viacheslav Smityukh Feb 22 '11 at 11:41
-
@RichTea - I never realised that. Just been looking at the docs and it seems that a bunch of overloads appeared in C# 4.0. Thanks for pointing that out! – sheikhjabootie Feb 22 '11 at 11:41
You should use Help.ShowHelp to do this
var chmFile = "CHM/test.chm";
Help.ShowHelp(ctrl, chmFile);
by default ShowHelp will search file in the application path

- 5,652
- 4
- 24
- 42
I can think of two options, depending on what you actually want. The first is to get the path to the resident directory (where the executing files live) and the second concerns the current directory (the directory specified for the executable to work from):
Resident directory:
var path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
Current directory:
var path = System.Environment.CurrentDirectory;
There is also another option, which, according to documentation gets the path for the executable file that started the application, not including the executable name
:
var path = Application.StartupPath;
As for constructing your path, other answers cover that very well indeed.

- 44,454
- 10
- 85
- 129
-
1Correct way is: AppDomain.CurrentDomain.SetupInformation.ApplicationBase – Viacheslav Smityukh Feb 22 '11 at 11:47
-
The easiest is
Process.Start("myHelpFile.chm");
This will open the chm
file in the systems default program associated with that file type.
This will work as long as the chm file is located in the same directory as your exe. If you have a sub folder called help, the path will be @"Help\myHelpFile.chm"
.

- 59,338
- 27
- 124
- 151