See Also: FileDialog Members
System.Windows.Forms.FileDialog is an abstract class that contains common behavior for the System.Windows.Forms.OpenFileDialog and System.Windows.Forms.SaveFileDialog classes. It is not intended to be used directly but contains common behavior for those two classes. You cannot create an instance of System.Windows.Forms.FileDialog. Although the class is declared public, you cannot inherit from it, as it contains internal abstract methods. To create a dialog box to select or save a file, use System.Windows.Forms.OpenFileDialog or System.Windows.Forms.SaveFileDialog.
System.Windows.Forms.FileDialog is a modal dialog box; therefore, when shown, it blocks the rest of the application until the user has chosen a file. When a dialog box is displayed modally, no input (keyboard or mouse click) can occur except to objects on the dialog box. The program must hide or close the dialog box (usually in response to some user action) before input to the calling program can occur.
When you use classes derived from System.Windows.Forms.FileDialog, such as System.Windows.Forms.OpenFileDialog and System.Windows.Forms.SaveFileDialog, avoid using string literals containing absolute paths. Instead, dynamically obtain the path using one or more of the techniques described in the following table.
If you want to enable users to select a folder instead of a file, use the System.Windows.Forms.FolderBrowserDialog.
Depending upon the type of application, how data associated with the application is stored, and the reason for accessing the file system, there are many possible ways in which you can create a directory path. The following table shows the techniques for creating paths dynamically.
Standard Windows paths, such as Program Files, MyDocuments, the Desktop and so on |
The Environment class is the most complete source for these, either through its static methods, such as Environment.SystemDirectory, or through the Environment.GetFolderPath(System.Environment.SpecialFolder) method, using one of the System.Environment.SpecialFolder enumerated values. |
Paths related to the current application |
The System.Windows.Forms.Application class has static members to obtain certain paths, such as Application.StartupPath, Application.ExecutablePath, Application.LocalUserAppDataPath, and Application.CommonAppDataPath. The System.IO.Path.GetTempPath method of the System.IO.Path returns the path of the temporary folder. The System.IO.Directory.GetCurrentDirectory method of the System.IO.Directory class returns the application's current executing directory. The System.IO.DriveInfo.RootDirectory property of the System.IO.DriveInfo class represents the specified drive's root directory. |
Paths stored as application settings |
Access the corresponding applications settings property of the wrapper class derived from System.Configuration.ApplicationSettingsBase. For more information, see Application Settings for Windows Forms. |
Registry storage |
Some applications store directory information in the registry. The System.Windows.Forms.Application class has the Application.CommonAppDataPath and Application.LocalUserAppDataPath properties that resolve to a Microsoft.Win32.RegistryKey value. |
ClickOnce applications |
For ClickOnce applications, use System.Windows.Forms.Application class members such as Application.UserAppDataPath, which will return a pointer to the ClickOnce data directory. For more information, see Accessing Local and Remote Data in ClickOnce Applications. |
International applications |
For international applications, retrieve the relative path portion from a string resource in your application by using the System.Resources.ResourceReader class. For more information about globalization and localization, see the topic Encoding and Localization. |
Notice that a full path may be built up using one or more of the described techniques. For example, the Environment.GetFolderPath(System.Environment.SpecialFolder) method might be used to obtain the path to the MyDocuments folder, then an application setting may be used to add a relative subdirectory portion.
The System.IO.Path class contains static members to assist in manipulating absolute and relative path strings, whereas the System.IO.File and System.IO.Directory classes have static members that actually manipulate files and directories, respectively.
If the user of your application changes the folder in the System.Windows.Forms.FileDialog, then the current working directory for your application is set to the location specified in the System.Windows.Forms.FileDialog. To prevent this, set the FileDialog.RestoreDirectory property to true.