OpenFileDialog.OpenFile OpenFileDialog.OpenFile OpenFileDialog.OpenFile OpenFileDialog.OpenFile Method

定義

ユーザーが選択したファイルを読み取り専用で開きます。Opens the file selected by the user, with read-only permission. このファイルは、FileName プロパティで指定されます。The file is specified by the FileName property.

public:
 System::IO::Stream ^ OpenFile();
public System.IO.Stream OpenFile ();
member this.OpenFile : unit -> System.IO.Stream
Public Function OpenFile () As Stream

戻り値

ユーザーが選択した読み取り専用ファイルを指定する StreamA Stream that specifies the read-only file selected by the user.

例外

ファイル名が null です。The file name is null.

次のコード例は、OpenFile メソッドの使用方法を示します。The following code example demonstrates how to use the OpenFile method.

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

注釈

OpenFileメソッドを使用してすばやくファイルを開く ダイアログ ボックスからの機能を提供します。The OpenFile method is used to provide a facility to quickly open a file from the dialog box. セキュリティのため読み取り専用モードで、ファイルが開きます。The file is opened in read-only mode for security purposes. 読み取り/書き込みモードで開くファイルには、する必要があります、別の方法など使用FileStreamします。To open a file in read/write mode, you must use another method, such as FileStream.

セキュリティ

FileDialogPermission
ファイルを開きます。to open a file. Open (関連する列挙体)Associated enumeration: Open.

適用対象

こちらもご覧ください