Share via


FileDialog.InitialDirectory Özellik

Tanım

Dosya iletişim kutusu tarafından görüntülenen ilk dizini alır veya ayarlar.

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

Özellik Değeri

Dosya iletişim kutusu tarafından görüntülenen ilk dizin. Varsayılan değer, boş dizedir ("").

Örnekler

Aşağıdaki kod örneği uygulamasını kullanır OpenFileDialogFileDialog ve oluşturma, özellikleri ayarlama ve iletişim kutusunu gösterme adımlarını gösterir. Örnek, iletişim kutusu kullanıcıya görüntülendiğinde ilk dizinin ne olduğunu ayarlamak için özelliğini kullanır InitialDirectory . Örnek, üzerine yerleştirilmiş bir Button form ve System.IO içine ad alanı eklenmesini gerektirir.

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

Açıklamalar

InitialDirectory özelliği genellikle aşağıdaki kaynaklardan biri kullanılarak ayarlanır:

  • Daha önce programda kullanılan ve belki de son dizinden veya dosya işleminden korunan bir yol.

  • Uygulama ayarı Registry , uygulamadaki bir veya dize kaynağı gibi kalıcı bir kaynaktan okunan yol.

  • Program Dosyaları, MyDocuments, MyMusic gibi standart Windows sistemi ve kullanıcı yolları (yöntemini kullanarak GetFolderPath elde edebilirsiniz)

  • Geçerli uygulamayla ilgili başlangıç dizini (nesnedeki Application özellikleri kullanarak elde edebilirsiniz) gibi bir yol.

Dinamik yollar oluşturma hakkında daha fazla bilgi için bkz. sınıfa FileDialog genel bakış.

Windows Vista'da, yalnızca bir dizin yolu yerine tam dosya adı olarak ayarlanırsa InitialDirectory , ilk dizin varsayılan olarak uygulama yoluna veya kullanıcının en son dosya seçtiği dizine ayarlanır.

Şunlara uygulanır

Ayrıca bkz.