OpenFileDialog.OpenFile メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > OpenFileDialog.OpenFile メソッドの意味・解説 

OpenFileDialog.OpenFile メソッド

ユーザーが選択したファイルを読み取り専用で開きます。このファイルは、FileName プロパティで指定されます。

名前空間: System.Windows.Forms
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文構文

例外例外
例外の種類条件

ArgumentNullException

ファイル名が null 参照 (Visual Basic では Nothing) です。

解説解説
使用例使用例

OpenFile メソッドを使用する方法を次のコード例に示します。この例では、ShowReadOnly プロパティが true に設定された OpenFileDialog を表示します。オプションをクリックしてファイルを読み取り専用モードで開くと、OpenFile メソッドを使用してファイルが開かれます。それ以外の場合は、FileStream クラスを使用して読み取り/書き込みモードでファイルが開かれます。

Private Function OpenFile() As
 FileStream

    ' Displays an OpenFileDialog and shows the read/only files.

    Dim DlgOpenFile As New
 OpenFileDialog()
    DlgOpenFile.ShowReadOnly = True
    Dim Fs As FileStream

    If DlgOpenFile.ShowDialog() = DialogResult.OK Then

        ' If ReadOnlyChecked is true, uses the OpenFile method to
        ' open the file with read/only access.

        If DlgOpenFile.ReadOnlyChecked = True
 Then

            Return DlgOpenFile.OpenFile()

            ' Otherwise, opens the file with read/write access.

        Else

            Dim Path As String
 = DlgOpenFile.FileName
            Return New FileStream(Path, System.IO.FileMode.Open,
 _
                    System.IO.FileAccess.ReadWrite)


        End If

    End If


End Function

private FileStream OpenFile()
{
    // Displays an OpenFileDialog and shows the read/only files.

    OpenFileDialog dlgOpenFile = new OpenFileDialog();
    dlgOpenFile.ShowReadOnly = true;


    if(dlgOpenFile.ShowDialog() == DialogResult.OK)
    {

        // If ReadOnlyChecked is true, uses the OpenFile method to
        // open the file with read/only access.

        if(dlgOpenFile.ReadOnlyChecked == true)
        {
            return (FileStream)dlgOpenFile.OpenFile();

        }

        // Otherwise, opens the file with read/write access.

        else
        {
            string path = dlgOpenFile.FileName;
            return new FileStream(path, System.IO.FileMode.Open
,
 
                    System.IO.FileAccess.ReadWrite);
        }


    }

    return null;


}

private:
   FileStream^ OpenFile()
   {
      // Displays an OpenFileDialog and shows the read/only files.
      OpenFileDialog^ dlgOpenFile = gcnew OpenFileDialog;
      dlgOpenFile->ShowReadOnly = true;
      if ( dlgOpenFile->ShowDialog() == ::DialogResult::OK
 )
      {
         // If ReadOnlyChecked is true, uses the OpenFile method to
         // open the file with read/only access.
         if ( dlgOpenFile->ReadOnlyChecked == true
 )
         {
            return dynamic_cast<FileStream^>(dlgOpenFile->OpenFile());
         }
         // Otherwise, opens the file with read/write access.
         else
         {
            String^ path = dlgOpenFile->FileName;
            return gcnew FileStream( path,System::IO::FileMode::Open,System::IO::FileAccess::ReadWrite
 );
         }
      }

      return nullptr;
   }
private FileStream OpenFile()
{
    // Displays an OpenFileDialog and shows the read/only files.
    OpenFileDialog dlgOpenFile = new OpenFileDialog();
    dlgOpenFile.set_ShowReadOnly(true);
    if (dlgOpenFile.ShowDialog().Equals(get_DialogResult().OK))
 {
        // If ReadOnlyChecked is true, uses the OpenFile method to
        // open the file with read/only access.
        if (dlgOpenFile.get_ReadOnlyChecked() == true)
 {
            return (FileStream)dlgOpenFile.OpenFile();
        }
        // Otherwise, opens the file with read/write access.
        else {
            String path = dlgOpenFile.get_FileName();
            return new FileStream(path, System.IO.FileMode.Open
,
                System.IO.FileAccess.ReadWrite);
        }
    }
    return null;
} //OpenFile 
.NET Framework のセキュリティ.NET Framework のセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
OpenFileDialog クラス
OpenFileDialog メンバ
System.Windows.Forms 名前空間
Stream
CheckFileExists
ReadOnlyChecked


このページでは「.NET Framework クラス ライブラリ リファレンス」からOpenFileDialog.OpenFile メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からOpenFileDialog.OpenFile メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からOpenFileDialog.OpenFile メソッド を検索

英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「OpenFileDialog.OpenFile メソッド」の関連用語

OpenFileDialog.OpenFile メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



OpenFileDialog.OpenFile メソッドのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS