共用方式為


FileDialog.InitialDirectory 屬性

定義

取得或設定檔案對話框中顯示的初始目錄。

public:
 property System::String ^ InitialDirectory { System::String ^ get(); void set(System::String ^ value); };
public string InitialDirectory { get; set; }
member this.InitialDirectory : string with get, set
Public Property InitialDirectory As String

屬性值

檔案對話框顯示的初始目錄。 預設為空字串(“”)。

範例

以下程式碼範例使用 OpenFileDialog 了實作 FileDialog 並說明了建立、設定屬性及顯示對話框的過程。 範例中使用 該 InitialDirectory 屬性來設定對話框顯示給使用者時的初始目錄。 這個範例需要一個表單上放置 a ButtonSystem.IO 加上命名空間。

private:
   void button1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      Stream^ myStream;
      OpenFileDialog^ openFileDialog1 = gcnew 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 )
      {
         if ( (myStream = openFileDialog1->OpenFile()) != nullptr )
         {
            // Insert code to read the stream here.
            myStream->Close();
         }
      }
   }
var fileContent = string.Empty;
var filePath = string.Empty;

using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
    openFileDialog.InitialDirectory = "c:\\";
    openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
    openFileDialog.FilterIndex = 2;
    openFileDialog.RestoreDirectory = true;

    if (openFileDialog.ShowDialog() == DialogResult.OK)
    {
        //Get the path of specified file
        filePath = openFileDialog.FileName;

        //Read the contents of the file into a stream
        var fileStream = openFileDialog.OpenFile();

        using (StreamReader reader = new StreamReader(fileStream))
        {
            fileContent = reader.ReadToEnd();
        }
    }
}

MessageBox.Show(fileContent, "File Content at path: " + filePath, MessageBoxButtons.OK);
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

備註

InitialDirectory該屬性通常透過以下來源之一設定:

  • 程式中先前使用的路徑,可能保留自上一個目錄或檔案操作。

  • 從持久性來源讀取的路徑,例如應用程式設定、A Registry 或應用程式中的字串資源。

  • 標準的 Windows 系統與使用者路徑,如程式檔案、MyDocuments、MyMusic 等(可透過此 GetFolderPath 方法取得)

  • 與目前應用程式相關的路徑,例如啟動目錄(你可以透過物件上的 Application 屬性取得)。

欲了解更多關於建立動態路徑的資訊,請參閱 FileDialog 類別總覽。

在 Windows Vista 中,如果 InitialDirectory 設定為完整檔名而非僅目錄路徑,初始目錄會預設為應用程式路徑,或使用者最後選擇檔案的目錄。

適用於

另請參閱