OpenFileDialog.ReadOnlyChecked Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Gets or sets a value indicating whether the read-only check box is selected.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public bool ReadOnlyChecked { get; set; }

Property Value

Type: System.Boolean

true if the read-only check box is selected; otherwise, false. The default value is false.

The ReadOnlyChecked state does not affect the read/write mode that OpenFileDialog.OpenFile uses to open a file selected in the dialog box. OpenFile will always open the file in read-only mode.

The ShowReadOnly property must be set before in order for the read-only check box to appear in the dialog box.

The following code example demonstrates the use of the ReadOnlyChecked property. This example displays the OpenFileDialog box with the ShowReadOnly property set to true. 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. 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 = 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.
                path = dlgOpenFile.FileName;
                return new FileStream(path, System.IO.FileMode.Open,
        } 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
Available since 1.1
Return to top