Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

OpenFileDialog.ShowReadOnly Property

Gets or sets a value indicating whether the dialog box contains a read-only check box.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
public bool ShowReadOnly { get; set; }

Property Value

Type: System.Boolean
true if the dialog box contains a read-only check box; otherwise, false. The default value is false.

The following code example demonstrates the use of the ShowReadOnly 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 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. 
            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;
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.