共用方式為


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 使用 屬性來設定使用者顯示對話方塊時的初始目錄。 此範例需要一個表單,其 Button 位於該表單上,以及 System.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 通常會使用下列其中一個來源來設定:

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

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

  • 標準 Windows 系統和使用者路徑,例如 Program Files、MyDocuments、MyMusic 等 (,您可以使用 GetFolderPath 方法取得)

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

如需建立動態路徑的詳細資訊,請參閱 FileDialog 類別概觀。

在 Windows Vista 上,如果 InitialDirectory 設定為完整檔案名,而不只是目錄路徑,則初始目錄預設為應用程式路徑,或使用者上次選取檔案的目錄。

適用於

另請參閱