OpenFileDialog.ShowReadOnly プロパティ

定義

ダイアログ ボックスに読み取り専用チェック ボックスが表示されているかどうかを示す値を取得または設定します。

public bool ShowReadOnly { get; set; }

プロパティ値

ダイアログ ボックスに読み取り専用チェック ボックスが表示されている場合は true。それ以外の場合はfalse。 既定値は false です。

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

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.
        string path = null;

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

            // Otherwise, opens the file with read/write access.
            else
            {
                path = dlgOpenFile.FileName;
                return new FileStream(path, System.IO.FileMode.Open,
                            System.IO.FileAccess.ReadWrite);
            }
        } catch (SecurityException ex)
            {
                // The user lacks appropriate permissions to read files, discover paths, etc.
                MessageBox.Show("Security error. Please contact your administrator for details.\n\n" +
                    "Error message: " + ex.Message + "\n\n" +
                    "Details (send to Support):\n\n" + ex.StackTrace
                );
            }
            catch (Exception ex)
            {
                // Could not load the image - probably related to Windows file system permissions.
                MessageBox.Show("Cannot display the image: " + path.Substring(path.LastIndexOf('\\'))
                    + ". You may not have permission to read the file, or " +
                    "it may be corrupt.\n\nReported error: " + ex.Message);
            }
    }

    return null;
}

適用対象

製品 バージョン
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

こちらもご覧ください