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

プロパティ値

ファイル ダイアログ ボックスに表示される起動ディレクトリ。 既定値は、空の文字列 ("") です。

次のコード例では、 および のFileDialog実装をOpenFileDialog使用して、プロパティの作成、設定、ダイアログ ボックスの表示を示します。 この例では、 プロパティを InitialDirectory 使用して、ダイアログ ボックスをユーザーに表示するときの初期ディレクトリを設定します。 この例では、 が配置され、名前空間が追加されたフォーム 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 通常、次のいずれかのソースを使用して設定されます。

  • プログラムで以前に使用されたパス。最後のディレクトリまたはファイル操作から保持されている可能性があります。

  • アプリケーション設定、またはアプリケーション内の文字列リソースなど、 Registry 永続的なソースから読み取られたパス。

  • プログラム ファイル、MyDocuments、MyMusic などの標準の Windows システムパスとユーザー パス (メソッドを GetFolderPath 使用して取得できます)

  • スタートアップ ディレクトリ (オブジェクトのプロパティを使用して取得できる) など、現在のアプリケーションに関連する Application パス。

動的パスの作成の詳細については、クラスの概要に関するページを FileDialog 参照してください。

Windows Vista では、 がディレクトリ パスではなく完全なファイル名に設定されている場合 InitialDirectory 、初期ディレクトリは既定でアプリケーション パス、またはユーザーが最後にファイルを選択したディレクトリに設定されます。

適用対象

こちらもご覧ください