OpenFileDialog Retains Data?

M

Martijn Mulder

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());
}
}
 
M

Michael A. Covington

Martijn Mulder said:
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?

As I recall, the "remembering" is done in the Registry, is keyed to the type
of file (the extension), and the exact behavior depends on the version of
Windows. Actually, I find it mildly annoying not to have more control over
it. There is a Knowledge Base article about it somewhere on
support.microsoft.com.

The .NET OpenFileDialog is merely a wrapper over the Win32 OpenFileDialog,
as I recollect.
 
L

Larry Smith

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.

"OpenFileDialog" and "SaveFileDialog" are just wrappers around
"GetOpenFileName()" and "GetSaveFileName()" in the WinAPI. The various
properties seen in the former two classes (such as "InitialDirectory")
really correspond to the appropriate member(s) of the OPENFILENAME structure
passed to the WinAPI functions (such as "lpstrInitialDir"). You can
therefore get a better understanding of the behaviour by reviewing the
OPENFILENAME structure directly (though MSFT should really publish this info
in the .NET docs). As for where this info is actually stored, it's unlikely
MSFT officially publishes it. Even if you can find a reference to a registry
key somewhere (presumably it exists here), you better make sure it's
official before you start monkeying around with it (which isn't a good idea
anyway - you should normally control things using the official API)
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Martijn Mulder said:
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?

Remember that the .net version of the dialog is just a wrap around the win32
version of it.
It's the unmanaged version the one that keep the reference of where it was
was the last time it was used.

You can rely upon it, but just remember this is a system wide setting, if
another program change it the next time yours call it it will have a
different location
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top