OpenFileDialog.ReadOnlyChecked OpenFileDialog.ReadOnlyChecked OpenFileDialog.ReadOnlyChecked OpenFileDialog.ReadOnlyChecked Property

定義

読み取り専用チェック ボックスがオンかオフかを示す値を取得または設定します。Gets or sets a value indicating whether the read-only check box is selected.

public:
 property bool ReadOnlyChecked { bool get(); void set(bool value); };
public bool ReadOnlyChecked { get; set; }
member this.ReadOnlyChecked : bool with get, set
Public Property ReadOnlyChecked As Boolean

プロパティ値

読み取り専用チェック ボックスがオンの場合は true。それ以外の場合は falsetrue if the read-only check box is selected; otherwise, false. 既定値は false です。The default value is false.

次のコード例は、の使用を示します、ReadOnlyCheckedプロパティ。The following code example demonstrates the use of the ReadOnlyChecked property. この例は、OpenFileDialogボックスで、ShowReadOnlyプロパティに設定trueします。This example displays the OpenFileDialog box with the ShowReadOnly property set to true. ユーザーが読み取り専用モードでファイルを開くためのオプションをクリックすると、ReadOnlyCheckedにプロパティが評価されるtrue、およびOpenFileメソッドを使用して、ファイルを開きます。If the user clicks the option to open the file in read-only mode, the ReadOnlyChecked property evaluates to true, and the OpenFile method is used to open the file. それ以外の場合、FileStreamクラスは、読み取り/書き込みモードでファイルを開きを使用します。Otherwise, the FileStream class is used to open the file in read/write mode.

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.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;
}


Private Function OpenFile() As FileStream

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

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

    If DlgOpenFile.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
        Dim path As New String("")

        ' If ReadOnlyChecked is true, uses the OpenFile method to
        ' open the file with read/only access.
        Try
            If (DlgOpenFile.ReadOnlyChecked = True) Then
                Return DlgOpenFile.OpenFile()
            Else
                ' Otherwise, opens the file with read/write access.
                Path = DlgOpenFile.FileName
                Return New FileStream(Path, System.IO.FileMode.Open, _
                        System.IO.FileAccess.ReadWrite)
            End If
        Catch SecEx As SecurityException
            ' 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: " & SecEx.Message * "\n\n" & _
                "Details (send to Support):\n\n" & SecEx.StackTrace)
        Catch Ex As Exception
            ' 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)
        End Try
    End If

    Return Nothing
End Function

注釈

ReadOnlyChecked状態では、読み取り/書き込みモードには影響しませんがOpenFileDialog.OpenFileを使用して、ダイアログ ボックスで選択されたファイルを開きます。The ReadOnlyChecked state does not affect the read/write mode that OpenFileDialog.OpenFile uses to open a file selected in the dialog box. OpenFile 読み取り専用モードで、ファイルが常に開きます。OpenFile will always open the file in read-only mode.

ShowReadOnly ダイアログ ボックスに表示される読み取り専用 チェック ボックスの順序で前にプロパティを設定する必要があります。The ShowReadOnly property must be set before in order for the read-only check box to appear in the dialog box.

適用対象

こちらもご覧ください