3

I have a OpenFileDialog which should open up a specific path say %ProgramData% when 'Browse' clicked for the first time the user uses the application. And for all successive terms, it should open up the last used folder.

I tried:

        OpenFileDialog ofd = new OpenFileDialog();
        ofd.InitialDirectory = "C:\ProgramData";
        ofd.RestoreDirectory = true;
        ofd.FileName = "";
        DialogResult dr = ofd.ShowDialog();

The problem here is, it opens up "C:\ProgramData" every time, even if I change path while looking for the required file. Is there a specific property that I should set or do I have to programmatically keep track of the usage of the OpenFileDialog and set path accordingly?

dushyantp
  • 4,398
  • 7
  • 37
  • 59

4 Answers4

2

Do something like this:

// save your current directory  
string currentDirectory = Environment.CurrentDirectory;  

// create an OpenFileDialog and set RestoreCurrentDirectory to false.   
OpenFileDialog ofd = new OpenFileDialog();  
ofd.RestoreCurrentDirectory = false;  
ofd.ShowDialog();  

// save the selected directory locally.   
string selectedDirectory = Environment.CurrentDirectory;  // OpenFileDialog changed this value.   
Environment.CurrentDirectory = currentDirectory; // reset the property with the first value.   

// next time you open an OpenFileDialog, set the InitialDirectory property  
OpenFileDialog ofd2 = new OpenFileDialog();  
ofd.InitialDirectory = selectedDirectory; // set the InitialDirectory to what it was last time an OpenFileDialog was opened.   
ofd.ShowDialog(); 

RestoreDirectory property makes sure that the value in Environment.CurrentDirectory will be reset before the OpenFileDialog closes. If RestoreDirectory is set to false, then Environment.CurrentDirectory will be set to whatever directory the OpenFileDialog was last open to.

eMi
  • 5,540
  • 10
  • 60
  • 109
  • Probably it would work, I chose to implement with Vinod's idea (check the selected answer). I had to make simple changes to existing code. Thanks for your time, it was helpful. – dushyantp Jul 16 '12 at 15:54
  • I just did some testing on Win7 64 and VS 2010, and as far as I can tell, OpenFileDialog has absolutely no effect on Environment.CurrentDirectory. – Tom Bushell Jan 08 '13 at 22:42
2

Try this:

you are resetting intialdirectory to C:\ProgramData on button click

public partial class Form1 : Form
    {
           OpenFileDialog ofd = new OpenFileDialog();

        public Form1()
        {
            InitializeComponent();
            ofd.InitialDirectory = "C:\\ProgramData";
        }    
        private void button1_Click(object sender, EventArgs e)
        {                     
          DialogResult dr = ofd.ShowDialog();
          ofd.InitialDirectory = null;   
        }    
    }
John Mitchell
  • 9,653
  • 9
  • 57
  • 91
Vinod
  • 4,672
  • 4
  • 19
  • 26
  • You found the issue, I was resetting the initial directory every time. I slightly changed your code and it finally worked for me. Thanks. – dushyantp Jul 16 '12 at 15:53
1

I think you are reading the RestoreDirectory property wrong. It, in fact, restores the directory to the default after the dialog is closed. Just the opposite to what you want to do.

Also check out: OpenFileDialog RestoreDirectory as no effect if Multiselect is set to true

Community
  • 1
  • 1
Gerald Versluis
  • 30,492
  • 6
  • 73
  • 100
-1

Simply,

Set FileDialog.RestoreDirectory Property true. When reopenning the file dialog box, it locates the last directory.

Example :

ofd . RestoreDirectory = true;
Ahmed Ghoneim
  • 6,834
  • 9
  • 49
  • 79
  • Will not work as OpenFileDialog.RestoreDirectory is not implemented – ZafarYousafi Jul 16 '12 at 11:58
  • This is what VS tells u when u hover over the property. Don't know why it is saying and it seems true as I have seen no change even if the property value is changed – ZafarYousafi Jul 16 '12 at 12:13