Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

OpenFileDialog Retains Data?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Martijn Mulder - 24 Sep 2007 13:36 GMT
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());
 }
}
Michael A. Covington - 24 Sep 2007 14:55 GMT
> It strikes me that System.Windows.Forms.OpenFileDialog seems te 'remember'
> which directory it was in last, even when a new OpenFileDialog-object is
[quoted text clipped - 6 lines]
> 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.
Larry Smith - 24 Sep 2007 15:09 GMT
> It strikes me that System.Windows.Forms.OpenFileDialog seems te 'remember'
> which directory it was in last, even when a new OpenFileDialog-object is
[quoted text clipped - 12 lines]
> 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)
Ignacio Machin ( .NET/ C# MVP ) - 24 Sep 2007 15:11 GMT
Hi,

> It strikes me that System.Windows.Forms.OpenFileDialog seems te 'remember'
> which directory it was in last, even when a new OpenFileDialog-object is
[quoted text clipped - 6 lines]
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.