OpenFileDialog.ReadOnlyChecked 속성

정의

읽기 전용 확인란이 선택되었는지 여부를 나타내는 값을 가져오거나 설정합니다.

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

속성 값

Boolean

읽기 전용 확인란이 선택되어 있으면 true이고, 선택되지 않으면 false입니다. 기본값은 false입니다.

예제

다음 코드 예제에서는 ReadOnlyChecked 속성입니다. 다음은 속성이 OpenFileDialog .로 설정된 상자를 ShowReadOnly 표시하는 예제입니다 true. 사용자가 읽기 전용 모드에서 파일을 여는 옵션을 클릭하면 속성이 ReadOnlyChecked 평가 true되고 OpenFile 메서드가 파일을 여는 데 사용됩니다. 그렇지 않으면 클래스가 FileStream 읽기/쓰기 모드로 파일을 여는 데 사용됩니다.

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 에 영향을 주지 않습니다. OpenFile 는 항상 읽기 전용 모드로 파일을 엽니다.

ShowReadOnly 대화 상자에 읽기 전용 확인란을 표시하려면 먼저 속성을 설정해야 합니다.

적용 대상

추가 정보