Opening in the same directory as the last time while using a Windows Forms app

BRS0903

Member
Joined
Jun 12, 2006
Messages
11
Programming Experience
3-5
I am developing a windows form application. I want to be able to have the OpenFiledialog to remember what directory was used the last time I opened a file using that particular OpenFileDialog box. How would I code my openfiledialog or windows form to remember what directory was used the last time I opened a file?
 
you could use an appconfig file, textfile, .dat file, xml file. Any of these would be ok to store such a thing. I suppose the prefrerred method would be an appconfig file but as i say you could use any of the above.

edit: i'm not sure but the openfiledialog may even expose a dynamic property would do just that. I havent looked into it so i can't be sure.
 
Each time that dialog is displayed and gives DialogResult.OK you save the current path in the preferred application settings storage. The place to store such setting change with the times, ini/txt/xml files and registry is commonly used. .Net 2.0 expose the easy to use My.Settings object for this.
 
Add an OpenFileDialog to a form in the designer. Go to the Properties window and expand the (ApplicationSettings) section. Click on (PropertyBindings) and then click the Browse (...) button that appears. Scroll to InitialDirectory, click it, click the drop-down button that appears and click New. Give the setting an appropriate name and click OK twice. You have now created an application setting and bound the InitialDirectory property of your OFD to it. Now double-click the OFD to create an event handler for the FileOK event. In the event handler put this line:
VB.NET:
Me.OpenFileDialog1.InitialDirectory = IO.Path.GetFolderName(Me.OpenFileDialog1.FileName)
substituting the name of your dialogue. Now the stored setting value will be loaded each time the app starts. Each time the user selectes a file the InitialDirectory property will be updated and that value will be saved to the application setting when the app shuts down.
 
Back
Top