OpenFileDialog Save Last Directory for each event
-
Monday, February 22, 2010 5:12 PM
I currently have 3 buttons I use to select and open various text files using openfiledialog (see code below). Although openfiledialog will remember the last directory that was selected by each button event, since each button actually is used to open files from 3 different directories, I would like each instance of openfiledialog to remember the path it last visited. How can I best acheive this? Thanks in advance for any help.
Private Sub LoadBTN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadBTN.Click Dim ofd_load As New OpenFileDialog() ofd_load.Filter = "TXT Files (*.TXT)|*.TXT" If ofd_load.ShowDialog = Windows.Forms.DialogResult.OK Then CurrentFile = ofd_load.FileName ReadFile(ofd_load.FileName) Else Exit Sub End If End Sub Private Sub SaveBTN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveBTN.Click Dim ofd_save As New OpenFileDialog() ofd_save.Filter = "TXT Files (*.TXT)|*.TXT" If ofd_save.ShowDialog = Windows.Forms.DialogResult.OK Then CurrentFile = ofd_save.FileName ReadFile(ofd_save.FileName) Else Exit Sub End If End Sub Private Sub RunBTN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RunBTN.Click Dim ofd_run As New OpenFileDialog() ofd_run .filter= "TXT Files (*.TXT)|*.TXT" If ofd_run .ShowDialog = Windows.Forms.DialogResult.OK Then CurrentFile = ofd_run .FileName ReadFile(ofd_run .FileName) Else Exit Sub End If End Sub
All Replies
-
Monday, February 22, 2010 5:28 PM
if I'm understanding this correctly, you can get the path of the selected file with the Path class and set it to a class level variable. Then you can use that next time you call the OpenFileDialog.
Try this in an app with 2 buttons. click button1 and select a file. then click button2
Dim ofdPath As String Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim ofd_load As New OpenFileDialog() ofd_load.Filter = "TXT Files (*.TXT)|*.TXT" If ofd_load.ShowDialog = Windows.Forms.DialogResult.OK Then ofdPath = Path.GetDirectoryName(ofd_load.FileName) Else Exit Sub End If End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click MessageBox.Show(ofdPath) End Sub -
Monday, February 22, 2010 6:15 PMI see how this grabs the path from the opened file but I'm not sure how to pass this path to the OpenFileDialog the next time it is called.
-
Monday, February 22, 2010 6:24 PM
have you looked at the properties for the OpenFileDialog?
there is one called InitialDirectory:
Dim ofd_load As New OpenFileDialog() ofd_load.Filter = "TXT Files (*.TXT)|*.TXT" ofd_load.InitialDirectory = ofdPath -
Monday, February 22, 2010 6:46 PM
If I understand well, you want that each button open the openfiledialog with 'its' distinct initialdirectory,
these different values can be assigned the button.tag property.
Private Sub Button_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) _ Handles button1.Click, button2.Click, button3.Click Dim ofd As New OpenFileDialog ofd.InitialDirectory = DirectCast(sender, Button).Tag.ToString 'etcetera End Sub
please, mark this as answer if it is THE answer
----------------
Diego Cattaruzza
Microsoft MVP - Visual Basic: Development
blog: http://community.visual-basic.it/Diego
web site: http://www.visual-basic.it
-
Monday, February 22, 2010 7:43 PMI can get the each openfiledialog to retain its' current directory but it won't remember it the next time the form/program is executed. That is, each time the form/program is executed it defaults to: C:\test\input (this was the directory selected by 3 of 4 buttons) for all 4 of the buttons even though one of the buttons selected C:\test\output the last time the program/form was executed. In the end, it would be great if each button remembered the last directory it accessed and carried that through each time the program/form was run. Any suggestions on how this can be achieved?
-
Monday, February 22, 2010 8:55 PM
use a Setting of type string
create a setting for each button and store the Path there. Then use the setting as the ofd.InitialPath
using the above code example, try this:
right click your app name in Solution Explorer, click on the Settings tab
Name = Button1Path
Type = String
Scope = User
then use this:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim ofd_load As New OpenFileDialog() ofd_load.Filter = "TXT Files (*.TXT)|*.TXT" ofd_load.InitialDirectory = My.Settings.Button1Path If ofd_load.ShowDialog = Windows.Forms.DialogResult.OK Then My.Settings.Button1Path = Path.GetDirectoryName(ofd_load.FileName) Else Exit Sub End If End Sub- Marked As Answer by terapinstation Monday, February 22, 2010 9:54 PM
-
Monday, February 22, 2010 10:09 PMVery cool. This worked like a charm and taught me a lot about establishing application settings that carry across time. Thanks for your help with this.

