Windows > Windows Forms Forums > Windows Forms General > OpenFileDialog Retains Data?
Ask a questionAsk a question
 

AnswerOpenFileDialog Retains Data?

  • Monday, September 24, 2007 12:14 PMTile Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    It strikes me that System.Windows.Forms.OpenFileDialog seems te 'remember' which directory it was in last, even when a new OpenFileDialog-object is created for every access to the file system. This 'persistence' also holds between different invocations of the same program. Even when the .cs file is re-compiled, the OpenFileDialog again opens in the directory where it was directed to the previous time.

    1. Is this a 'feature' of OpenFileDialog that I can rely upon?
    2. If it is a feature, where and how does the OpenFileDialog store its data?

    Try it with the small stand-alone program below. Navigate to some directory and double click an existing file or click 'OK'. Clicking 'Cancel' or hitting 'Escape' does not take you to the new directory. Then push the Open File... button again and you find yourself in the previous directory. Cool, but strange.



    using System;
    using System.Windows.Forms;

    class Form1:Form
    {

     Form1()
     {
      Controls.Add(new Button());
      Controls[0].Text="File Open...";
      Controls[0].Click+=ButtonClick;
     }

     void ButtonClick(object a,EventArgs b)
     {
      new OpenFileDialog().ShowDialog();
     }

     [System.STAThread]
     static void Main()
     {
      Application.Run(new Form1());
     }
    }



Answers

  • Monday, September 24, 2007 4:46 PMnobugzMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Yes, if you do not set the InitialDirectory property, OpenFileDialog looks in the registry for the last path that was used to open a file.  I think the key is HKCU\ Software\ Microsoft\ Windows\ CurrentVersion\ Explorer\ Comdlg32\ OpenSaveMRU.  You shouldn't rely on this history but it is convenient to the user if you can't provide a decent InitialDirectory value.

All Replies

  • Monday, September 24, 2007 2:00 PMH. _冬_ Tony Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    what version of .net are you using?

    I seem to remember what you mentioned but I tried the following under vs.net 2005 with .net 2.0 but it always opens the dialog in the .exe directory. (when I run it under debug in vs.net, it's the ...\DEBUG directory)

                OpenFileDialog form = new OpenFileDialog();
                form.ShowDialog();
  • Monday, September 24, 2007 4:46 PMnobugzMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Yes, if you do not set the InitialDirectory property, OpenFileDialog looks in the registry for the last path that was used to open a file.  I think the key is HKCU\ Software\ Microsoft\ Windows\ CurrentVersion\ Explorer\ Comdlg32\ OpenSaveMRU.  You shouldn't rely on this history but it is convenient to the user if you can't provide a decent InitialDirectory value.
  • Wednesday, March 26, 2008 6:59 PMQwertie Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I looked at the BCL source code and it appears that InitialDirectory is passed unchanged to GetOpenFileName in the lpstrInitialDir field of OPENFILENAME, the documentation for which states:

    Pointer to a NULL terminated string that can specify the initial directory. The algorithm for selecting the initial directory varies on different platforms.

    Windows 2000/XP:

    1. If lpstrFile contains a path, that path is the initial directory.
    2. Otherwise, lpstrInitialDir specifies the initial directory.
    3. Otherwise, if the application has used an Open or Save As dialog box in the past, the path most recently used is selected as the initial directory. However, if an application is not run for a long time, its saved selected path is discarded.
    4. If lpstrInitialDir is NULL and the current directory contains any files of the specified filter types, the initial directory is the current directory.
    5. Otherwise, the initial directory is the personal files directory of the current user.
    6. Otherwise, the initial directory is the Desktop folder.