Results 1 to 8 of 8

Thread: reset "OpenFileDialogbox" directory

  1. #1

    Thread Starter
    Member Shadow45o's Avatar
    Join Date
    Sep 2008
    Posts
    51

    Question reset "OpenFileDialogbox" directory

    I have searched and have tryed anything I could think of...but I can't figure this out...What I need to know is if there is a way to reset the directory of the dialogbox so each time it is opened, it will always go to the "Initial Directory".

    The only way I know it will reset is if the app is reset...but I dn't want to have to close the app each time.

    Thanks for any help

  2. #2
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: reset "OpenFileDialogbox" directory

    I must be missing something here... the OpenFileDialog has an "InitialDirectory" property which is the folder which is shown each time the dialog is shown.

    Is that not what you want?

  3. #3

    Thread Starter
    Member Shadow45o's Avatar
    Join Date
    Sep 2008
    Posts
    51

    Re: reset "OpenFileDialogbox" directory

    Quote Originally Posted by keystone_paul View Post
    I must be missing something here... the OpenFileDialog has an "InitialDirectory" property which is the folder which is shown each time the dialog is shown.

    Is that not what you want?
    The "InitialDirectory" property just sets the folder at RunTime, but I want it to keep the OpenFileDialog to keep opening at the Initial folder instead of needing to close the program and open it to reset it...

    The short version...the initial directory only sets it once, then the dialogbox remembers the last spot opened, but I don't want it to, I just want it to have 1 set folder to open up at...
    hope this clears things up...

  4. #4

  5. #5
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: reset "OpenFileDialogbox" directory

    Hmm.. unfortuately my laptop has died tonight and I haven't got Visual Studio reinstalled yet but I must admit I though Initial Directory was applied each time the dialog was shown.

    If not... can you not just create a new instance of the openfiledialog in code (rather than via the designer) and then dispose of the object, so that every time you create the dialog it is a totally new instance, or are you saying that for the duration of the "session" of the application the initialdirectory never gets reapplied?

    EDIT - or do what NickThissen said!

  6. #6
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: reset "OpenFileDialogbox" directory

    Hmm, it seems that property does not do what I expected it to do. At least, even when True it still remembers the location of the last file you opened and shows that instead of the InitialDirectory.

    Paul's suggestion however does work. Instead of dropping an OpenFileDialog component on your form, you can create one during run-time. You can use a Using block to make it easier on yourself. Any closing and disposing is done automatically (even when an error occurs). You can also use the With {..} initializer syntax to set it's properties:
    vb.net Code:
    1. Using ofd As New OpenFileDialog With {.InitialDirectory = "C:", _
    2.                                               .Multiselect = False, _
    3.                                               .Filter = "Text files|*.txt", _
    4.                                               .Title = "Select a file."}
    5.  
    6.             If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
    7.                 '...
    8.             End If
    9.  
    10.         End Using

    This way, a new instance is created every time, and it does not remember the last location.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: reset "OpenFileDialogbox" directory

    The RestoreDirectory property determines whether or not the Environment.CurrentDirectory property is restored when the dialogue is closed or not. When you start an application it working directory will be the startup folder by default. That can be changed by setting the Working Directory property of a Windows shortcut or using a ProcessStartInfo when calling Process.Start. When you open a FileDialog and navigate the working directory follows that navigation. When you close the FileDialog the working directory can either stay where it is or be restored to its original value, depending on the RestoreDirectory property.

    When you set the InitialDirectory property of a FileDialog in the designer, that value remains for the life of that object unless you explicitly change it. As such that will be the initial folder every time you open that FileDialog. Now, if you navigate to a file in that dialogue and then close it, the FileName remains. The next time you open the dialogue it will open to that same path, overriding the InitialDirectory. If you want to open the dialogue at the InitialDirectory path you simply set the FileName to Nothing before calling ShowDialog.

    Having said that, I just tested in Vista and the InitialDirectory was always honoured anyway, with the last file path being shown in full in the address field.

  8. #8
    Hyperactive Member
    Join Date
    May 2007
    Posts
    300

    Re: reset "OpenFileDialogbox" directory

    Quote Originally Posted by jmcilhinney View Post
    When you set the InitialDirectory property of a FileDialog in the designer, that value remains for the life of that object unless you explicitly change it. As such that will be the initial folder every time you open that FileDialog. Now, if you navigate to a file in that dialogue and then close it, the FileName remains. The next time you open the dialogue it will open to that same path, overriding the InitialDirectory. If you want to open the dialogue at the InitialDirectory path you simply set the FileName to Nothing before calling ShowDialog.

    Having said that, I just tested in Vista and the InitialDirectory was always honoured anyway, with the last file path being shown in full in the address field.
    I love the way, when NOT using the InitialDirectory setting, that the OpenFileDialog automagically "remembers" where you last had it pointed, even after closing the program and opening it again. No need to add a setting to the program to store the default directory, vb takes care of it behind the scenes.

    So where does vb store this "last opened path"? I need to access it so that when the user opens the dialog, it selects the newest file. (I have the code for finding the latest file, but I can't find what path the openfiledialog will open to before I open it). I thought that it might set "InitialDirectory" itself, but it doesn't.

    Edit:
    FYI - The "Restore Directory" setting is only valid for SaveFileDialog, it does nothing on OpenFileDialog.
    Last edited by Meestor_X; Sep 20th, 2009 at 02:24 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width