title: Keep the last selected path using OpenFileDialog / C# / .Net
body: <p>I want to keep the last path which is selected. This is the code: </p>
  
  <pre><code>private void testFileButton_Click(object sender, EventArgs e)
  {
       fd = new OpenFileDialog();
       fd.FileName = testParameters.testFileFile;
       fd.InitialDirectory = testParameters.testFileDir;
  
       if (fd.ShowDialog() == DialogResult.OK)
       {                    
           try
           {
              if (fd.SafeFileName != null)
               {
                   testParameters.testFileDir = Path.GetDirectoryName(fd.FileName);
                   testParameters.testFileFile = Path.GetFileName(fd.FileName);
                   testFileLabel.Text = fd.FileName;                 
               }           
            }
            catch (IOException)
            {
                MessageBox.Show("Error: Could not read file");
            }
        }
  } 
  </code></pre>
  
  <p>to be able to keep the last selected path, I tried to add RestorDirectory and index but I did not get any result:</p>
  
  <pre><code>private void testFileButton_Click(object sender, EventArgs e)
  {
       fd = new OpenFileDialog();
       fd.FileName = testParameters.testFileFile;
       fd.InitialDirectory = testParameters.testFileDir;
       fd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
       fd.FilterIndex = 2;
       fd.RestoreDirectory = true;
  
       if(...){
       ...
       }
  }
  </code></pre>
  
  <p>then I tried to use "else" instead:</p>
  
  <pre><code>private void testFileButton_Click(object sender, EventArgs e)
  {
      fd = new OpenFileDialog();
      fd.FileName = testParameters.testFileFile;
      fd.InitialDirectory = testParameters.testFileDir;
  
       if (fd.ShowDialog() == DialogResult.OK)
       {                    
           try
           {
               if (fd.SafeFileName != null)
               {
                   testParameters.testFileDir = Path.GetDirectoryName(fd.FileName);
                   testParameters.testFileFile = Path.GetFileName(fd.FileName);
                   testFileLabel.Text = fd.FileName;                 
               }  
           } 
  
  
       }
       catch (IOException)
       {
           MessageBox.Show("Error: Could not read file");
       }
  
  
      }
      else
      {
           openFileDialog1.InitialDirectory = @"C:\";
      }
  } 
  </code></pre>
  
  <p>but again without any result..</p>
  
  <p>Does anyone have any idea?</p>
  
  <p>Editing: Last attemp</p>
  
  <pre><code>private void testFileButton_Click(object sender, EventArgs e)
          {
               //http://stackoverflow.com/questions/7793158/obtaining-only-the-filename-when-using-openfiledialog-property-filename
               OpenFileDialog fd = new OpenFileDialog();
               fd.FileName = testParameters.testFileFile;
               Environment.CurrentDirectory = @"C:\" ;
  
               if (fd.ShowDialog() == DialogResult.OK )
               {                    
                   try
                   {
                      if (fd.SafeFileName != null)
                       {
                           string ffileName = fd.FileName;
                           testParameters.testFileDir = Path.GetDirectoryName(ffileName);
                           testParameters.testFileFile = Path.GetFileName(ffileName);
                           testFileLabel.Text = ffileName;
  
                       }
  
                    }
                    catch (IOException)
                    {
                        MessageBox.Show("Error: Could not read file");
                    }
                }
          }   
  </code></pre>