方法 : OpenFileDialog コンポーネントを使用してファイルを開く

OpenFileDialog コンポーネントを使用すると、ユーザーは、自分のコンピューターやネットワーク上の任意のコンピューターのフォルダーを参照し、1 つ以上のファイルを選択して開くことができます。 このダイアログ ボックスは、ユーザーがダイアログ ボックス内で選択したファイルのパスと名前を返します。

ユーザーが開くファイルを選択したら、そのファイルを開く方法として 2 とおりのアプローチがあります。 ファイル ストリームを使用する場合は、StreamReader クラスのインスタンスを作成します。 もう 1 つの方法は、OpenFile メソッドを使用して、選択されたファイルを開く方法です。

次に示す最初の例には、FileIOPermission のアクセス許可チェック (以降の「セキュリティに関するメモ」を参照) が含まれますが、許可されるのはファイル名へのアクセスです。 この手法は、ローカル コンピューター、イントラネット、およびインターネットの各ゾーンから利用できます。 2 番目の方法でも FileIOPermission のアクセス許可チェックが行われますが、この方法はイントラネット ゾーンまたはインターネット ゾーンのアプリケーションに適しています。

OpenFileDialog コンポーネントを使用してファイルをストリームとして開くには

  • [ファイルを開く] ダイアログ ボックスを表示し、ユーザーによって選択されたファイルを開くメソッドを呼び出します。

    1 つのアプローチとしては、ShowDialog メソッドを使用して [ファイルを開く] ダイアログ ボックスを表示し、StreamReader クラスのインスタンスを使用してファイルを開きます。

    次のコード例では、Button コントロールの Click イベント ハンドラーを使用して OpenFileDialog コンポーネントのインスタンスを開いています。 ファイルが選択され、ユーザーが [OK] をクリックすると、ダイアログ ボックスで選択されたファイルが開きます。 この場合、ファイルの内容はメッセージ ボックスに表示され、ファイル ストリームが読み取られたことが単に示されます。

    セキュリティに関するメモセキュリティに関するメモ

    FileName プロパティを取得または設定するには、アセンブリに対して、System.Security.Permissions.FileIOPermission クラスから特権レベルが与えられている必要があります。 完全には信頼できないコンテキストでプロセスを実行している場合は、権限不足のため例外がスローされることがあります。 詳細については、「コード アクセス セキュリティの基礎」を参照してください。

    この例のコードは、フォームに Button コントロールと OpenFileDialog コンポーネントがあることを想定して書かれています。

    Private Sub Button1_Click(ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles Button1.Click
       If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
         Dim sr As New System.IO.StreamReader(OpenFileDialog1.FileName)
         MessageBox.Show(sr.ReadToEnd)
         sr.Close()
       End If
    End Sub
    
    private void button1_Click(object sender, System.EventArgs e)
    {
       if(openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
       {
          System.IO.StreamReader sr = new 
             System.IO.StreamReader(openFileDialog1.FileName);
          MessageBox.Show(sr.ReadToEnd());
          sr.Close();
       }
    }
    
    private:
       void button1_Click(System::Object ^ sender,
          System::EventArgs ^ e)
       {
          if(openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
          {
             System::IO::StreamReader ^ sr = gcnew
                System::IO::StreamReader(openFileDialog1->FileName);
             MessageBox::Show(sr->ReadToEnd());
             sr->Close();
          }
       }
    

    (Visual C# および Visual C++) フォームのコンストラクターに次のコードを挿入してイベント ハンドラーを登録します。

    this.button1.Click += new System.EventHandler(this.button1_Click);
    
    this->button1->Click += gcnew
       System::EventHandler(this, &Form1::button1_Click);
    

    注意

    ファイル ストリームからの読み取りの詳細については、「FileStream.BeginRead メソッド」および「FileStream.Read メソッド」を参照してください。

OpenFileDialog コンポーネントを使用してファイルをファイルとして開くには

  • ShowDialog メソッドを使用してダイアログ ボックスを表示し、OpenFile メソッドを使用してファイルを開きます。

    OpenFileDialog コンポーネントの OpenFile メソッドは、ファイルを構成するバイトを返します。 これらのバイトにより、読み取り元のストリームが提供されます。 次のコード例では、OpenFileDialog コンポーネントが "カーソル" フィルターと共にインスタンス化され、ユーザーが拡張子 .cur のファイルだけを選択できるようになっています。 .cur ファイルが選択されると、フォームのカーソルが選択されたカーソルに設定されます。

    セキュリティに関するメモセキュリティに関するメモ

    OpenFile メソッドを呼び出すには、アセンブリに対して System.Security.Permissions.FileIOPermission クラスで特権レベルが許可されている必要があります。 完全には信頼できないコンテキストでプロセスを実行している場合は、権限不足のため例外がスローされることがあります。 詳細については、「コード アクセス セキュリティの基礎」を参照してください。

    この例のコードは、フォームに Button コントロールがあることを想定して書かれています。

    Private Sub Button1_Click(ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles Button1.Click
       ' Displays an OpenFileDialog so the user can select a Cursor.
       Dim openFileDialog1 As New OpenFileDialog()
       openFileDialog1.Filter = "Cursor Files|*.cur"
       openFileDialog1.Title = "Select a Cursor File"
    
       ' Show the Dialog.
       ' If the user clicked OK in the dialog and 
       ' a .CUR file was selected, open it.
       If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
         ' Assign the cursor in the Stream to the Form's Cursor property.
         Me.Cursor = New Cursor(openFileDialog1.OpenFile())
       End If
    End Sub
    
    private void button1_Click(object sender, System.EventArgs e)
    {
       // Displays an OpenFileDialog so the user can select a Cursor.
       OpenFileDialog openFileDialog1 = new OpenFileDialog();
       openFileDialog1.Filter = "Cursor Files|*.cur";
       openFileDialog1.Title = "Select a Cursor File";
    
       // Show the Dialog.
       // If the user clicked OK in the dialog and
       // a .CUR file was selected, open it.
        if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
       {
          // Assign the cursor in the Stream to the Form's Cursor property.
          this.Cursor = new Cursor(openFileDialog1.OpenFile());
       }
    }
    
    private:
       void button1_Click(System::Object ^ sender,
          System::EventArgs ^ e)
       {
          // Displays an OpenFileDialog so the user can select a Cursor.
          OpenFileDialog ^ openFileDialog1 = new OpenFileDialog();
          openFileDialog1->Filter = "Cursor Files|*.cur";
          openFileDialog1->Title = "Select a Cursor File";
    
          // Show the Dialog.
          // If the user clicked OK in the dialog and
          // a .CUR file was selected, open it.
          if (openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
          {
             // Assign the cursor in the Stream to
             // the Form's Cursor property.
             this->Cursor = gcnew
                System::Windows::Forms::Cursor(
                openFileDialog1->OpenFile());
          }
       }
    

    (Visual C# および Visual C++) フォームのコンストラクターに次のコードを挿入してイベント ハンドラーを登録します。

    this.button1.Click += new System.EventHandler(this.button1_Click);
    
    this->button1->Click += gcnew
       System::EventHandler(this, &Form1::button1_Click);
    

参照

参照

OpenFileDialog

その他の技術情報

OpenFileDialog コンポーネント (Windows フォーム)