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

속성 값

String

파일 대화 상자가 처음에 표시하는 디렉터리입니다. 기본값은 빈 문자열("")입니다.

예제

다음 코드 예제에서는 구현 FileDialogOpenFileDialog 사용 하 고 만들기를 보여 줍니다., 속성의 설정 및 대화 상자를 표시 합니다. 이 예제에서는 이 속성을 사용하여 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 초기 디렉터리가 기본적으로 애플리케이션 경로 또는 사용자가 마지막으로 파일을 선택한 디렉터리로 설정됩니다.

적용 대상

추가 정보