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)

'Declaration
Public Property InitialDirectory As String
'Usage
Dim instance As FileDialog 
Dim value As String 

value = instance.InitialDirectory

instance.InitialDirectory = value

Property Value

Type: System.String
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 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 Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim myStream As Stream = Nothing 
    Dim openFileDialog1 As New OpenFileDialog()

    openFileDialog1.InitialDirectory = "c:\"
    openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
    openFileDialog1.FilterIndex = 2
    openFileDialog1.RestoreDirectory = True 

    If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then 
        Try
            myStream = openFileDialog1.OpenFile()
            If (myStream IsNot Nothing) Then 
                ' Insert code to read the stream here. 
            End If 
        Catch Ex As Exception
            MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
        Finally 
            ' Check this again, since we need to make sure we didn't throw an exception on open. 
            If (myStream IsNot Nothing) Then
                myStream.Close()
            End If 
        End Try 
    End If 
End Sub

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

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft