Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retaining default folder for OpenFileDialog 1

Status
Not open for further replies.

CaKiwi

Programmer
Apr 8, 2001
1,294
US
I have 2 browse buttons in my application which call an OpenFileDialog. I want the dialog from each button to retain the last folder the user browsed to as its initialdirectory the next time it is opened. I have tried using 2 OpenFileDialogs and playing around with the initialdirectory and restoredirectory properties, but nothing seems to work.

Any help would be greatly appreciated.

CaKiwi
 
Basically you need something like this:

Code:
        If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim s As String = OpenFileDialog1.FileName
            OpenFileDialog1.InitialDirectory = s.Substring(0, s.LastIndexOf("\"))
        End If
 
Thanks for the response.

What I really want is for 2 different OpenFileDiaglogs in my application to act as if they were they were completely separate, including remembering their initial directories from the previous execution of the program. If I use a method similar to the one you suggest, I would have to save the directories somewhere, perhaps in the registry. Is this the simplest way to do this? If it is, how do you suggest saving the data across program executions? Where can I find an example of saving a string in the registry?

Thanks for the help.

CaKiwi
 
I got what I want by saving the initialdirectory in the registry
Code:
        Dim str1 As String
        OpenFileDialog1.InitialDirectory = GetSetting("MyApp", "Misc", "InitialDirectory1")
        OpenFileDialog1.ShowDialog()
        str1 = OpenFileDialog1.FileName
        If (str1 <> "") Then
            str1 = str1.Substring(0, str1.LastIndexOf("\"))
            SaveSetting("MyApp", "Misc", "InitialDirectory1", str1)
        End If
I couldn't find the equivalent to VB6 "app.title" so I hard coded the key ("MyApp" in the above code snippet)

CaKiwi
 
app is now application.

You can create a setting (maybe 2) to save the strings. [You can have a look at MY namespace (if you are using 2k5 edition)]
 
application doesn't have a title field, but productname seems to be what I want.

Thanks for the help.

CaKiwi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top