Export (0) Print
Expand All

OpenFileDialog.ReadOnlyChecked Property

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)

'Declaration
Public Property ReadOnlyChecked As Boolean

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 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

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft