0

IN my application, I have two OpenFileDialogs in which the user selects different file types. The first openfiledialog is for selecting a config file located at directory A and the second dialog for selecting an xml file located at directory B.

My problem is when I tried to open one of the dialog boxes, it brings me to just one directory which is the directory for the xml files; never on the folder for config files. User has to navigate through the folders just to locate the folder for config files.

I tried this:

openFileDialog1.InitialDirectory = @"C:\Users\uidr3024\Downloads\Tool\cfg";

and this:

openFileDialog2.InitialDirectory = @"C:\Users\uidr3024\Downloads\Tool\XMLs";

and it worked. But what do I do when the app's used on a different computer then, with probably a different Folder name for "Tool" but still the same folder name for "cfg" and "XMLs".

I was thinking along the lines of GetFullPath and GetTempPath but I'm not sure.

Kurisuchin
  • 155
  • 1
  • 4
  • 20
  • First of all files should be located in a specified location rather than a custom location.You cannot retrieve the files if the files are accessed in another system as there wont be folders like Downloads – Tharif Mar 20 '15 at 09:40
  • Store the last-used directory yourself in an application setting. – Hans Passant Mar 20 '15 at 11:07
  • Sorry, it took long for me to respond. How do I exactly declare or set in App Settings the directory? – Kurisuchin Mar 27 '15 at 02:41

2 Answers2

0

use a relativ path inside your project structure. Or maybe the installation path. %programfiles%/myapp/tools/xml or C:\Users\%username%\AppData

Andre
  • 662
  • 1
  • 8
  • 19
0

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;
Community
  • 1
  • 1
Bernd Linde
  • 2,098
  • 2
  • 16
  • 22
  • Yes, in my sample code, the folder would be located in the Downloads folder. But what if another user happen to place it on another different folder, perhaps Documents? or Desktop? It varies, I'm not entirely sure where they would place the Tools folder. Does this mean that I have to tell the future users of this app to specify the folder's location? – Kurisuchin Mar 23 '15 at 01:31
  • Have a look at Hans Passants comment. Your comment changes many things and you will need to store the last user selected directory. You can however still use a central helper class if you manage to extract the root directory of where your Tools folder resides. – Bernd Linde Mar 23 '15 at 09:33