Export (0) Print
Expand All

FileDialog.InitialDirectory Property

Gets or sets the initial directory displayed by the file dialog box.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public string InitialDirectory { get; set; }
/** @property */
public String get_InitialDirectory ()

/** @property */
public void set_InitialDirectory (String value)

public function get InitialDirectory () : String

public function set InitialDirectory (value : String)

Not applicable.

Property Value

The initial directory displayed by the file dialog box. The default is an empty string ("").

The InitialDirectory property is typically set using one of the following sources:

  • A path that was previously used in the program, perhaps retained from the last directory or file operation.

  • A path read from a persistent source, such as an application setting, a Registry or a string resource in the application.

  • Standard Windows system and user paths, such as Program Files, MyDocuments, MyMusic, and so on (which you can obtain using the GetFolderPath method)

  • A path related to the current application, such as its startup directory (which you can obtain using properties on the Application object).

For more information about creating dynamic paths, see the FileDialog class overview.

On Microsoft Windows Vista, if InitialDirectory is set to a full file name instead of just a directory path, the initial directory will default either to the application path, or to the directory from which the user last selected a file.

The following code example uses the OpenFileDialog implementation of FileDialog and illustrates creating, setting of properties, and showing the dialog box. The example uses the InitialDirectory property to set what the initial directory is when the dialog box is displayed to the user. The example requires a form with a Button placed on it and the System.IO namespace added to it.

private void button1_Click(object sender, System.EventArgs e)
{
    Stream myStream = null;
    OpenFileDialog openFileDialog1 = new OpenFileDialog();
 
    openFileDialog1.InitialDirectory = "c:\\" ;
    openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
    openFileDialog1.FilterIndex = 2 ;
    openFileDialog1.RestoreDirectory = true ;

    if(openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        try
        {
            if ((myStream = openFileDialog1.OpenFile()) != null)
            {
                using (myStream)
                {
                    // Insert code to read the stream here.
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
        }
    }
}
    

protected void button1_Click(Object sender, System.EventArgs e)
{
    Stream myStream;
    OpenFileDialog openFileDialog1 = new OpenFileDialog();
    openFileDialog1.set_InitialDirectory("c:\\");
    openFileDialog1.set_Filter(
        "txt files (*.txt)|*.txt|All files (*.*)|*.*");
    openFileDialog1.set_FilterIndex(2);
    openFileDialog1.set_RestoreDirectory(true);
    if (openFileDialog1.ShowDialog().Equals(get_DialogResult().OK)) {
        if ((myStream = openFileDialog1.OpenFile()) != null) {
            // Insert code to read the stream here.
            myStream.Close();
        }
    }
} //button1_Click

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft