views:

45

answers:

1

In my application I have two places where the user needs to select a file. In both cases, the files are in different directories, but they are generally the same between runs.

The OpenFileDialog appears to be defaulting to the last directory used, but this is useless to me since it is almost always the wrong folder, and I end up alternating between the two folders. What I would like is to somehow have the first dialog remember the path that was used the last time it was opened, and the second to remember its own path as well.

Example: Path A is C:\foo\bar\something\x.dll, Path B is C:\foo\baz\whatever\y.xml

Dialog a opens and I select A, then later dialog b opens(defaulted to A) and I have to navigate back and up to B. When I open the app again dialog a opens (defaulted to B) and I have to navigate back up to A again.

I would like to avoid all of this extra navigation by remembering the paths separately. Is there a good way to do that?

+3  A: 

When you open each dialog, just set the dialog's InitialDirectory property to the folder that was last used for that dialog.

Granted, this will require saving the directory for each dialog, but it will provide the behavior you are looking to achieve.

Reed Copsey
I like it, but I am having a little trouble extracting the path after the dialog closes. I can get the filename, but is there a way to get the final path besides trimming to the last /?
CaptnCraig
@CaptnCraig: Use System.IO.Path.GetDirectoryName(theFilename); See: http://msdn.microsoft.com/en-us/library/system.io.path.getdirectoryname.aspx
Reed Copsey
Thanks! Worked fine.
CaptnCraig