As mentioned in comments, you should not use an absolute path.
The .Net framework has some functionality to retrieve variable paths for you, that will be user specific, rather use these for your storage/loading locations.
The common folder paths can be obtained by using Environment.GetFolderPath()
method. This will return you the system defined location for various things. This will ensure that you never have to deal with figuring out if a directory is correct or different on machines, since your references will always be relative and not absolute.
In your case, you could use it to access a profile specific folder for your items
// Use the Environment.GetFolderPath with Environment.SpecialFolder.ApplicationData
// to retrieve the %userprofile%\AppData\Roaming folder for the user
// Also use Path.Combine to link that path to your relative paths.
openFileDialog1.InitialDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"<YourAppName>\Tools\cfg");
openFileDialog1.InitialDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"<YourAppName>\Tools\XML");
Since you are using the Downloads
folder in your sample code, you will notice that there is no special folder enumerator for the Downloads
directory, which has been commented and asked about a few times on SO.
The best solution I have found, which will also be correct when used on other language setups can be found in this answer.
Utilizing the code/class that you can find in that answer, you can retrieve the Downloads
folder for the user profile with the simple code of
openFileDialog1.InitialDirectory = Path.Combine(KnownFolders.GetDefaultPath(KnownFolder.Downloads), @"<YourAppName>\Tools\cfg");
openFileDialog1.InitialDirectory = Path.Combine(KnownFolders.GetDefaultPath(KnownFolder.Downloads), @"<YourAppName>\Tools\XML");
Extra info:
I would recommend that you have a central place to either 'store' or generate your folder names. This will ensure that you have only one place that you would need to add or change your folder names, instead of having to search through your entire codebase for the usage of a specific string.
A small example of a helper class that you could use/extend for this purpose:
public static class DirectoryStrings
{
private const string AppFolderName = "MyAppName";
public static string ToolsFolderName
{
get { return Path.Combine(KnownFolders.GetDefaultPath(KnownFolder.Downloads), AppFolderName, "Tools"); }
}
public static string XmlFolderName
{
get { return Path.Combine(ToolsFolderName, "XML"); }
}
public static string CfgFolderName
{
get { return Path.Combine(ToolsFolderName, "cfg"); }
}
}
This then allows for clean code of
openFileDialog1.InitialDirectory = DirectoryStrings.XmlFolderName;