OpenFileDialog.ReadOnlyChecked 屬性

定義

取得或設定值,表示是否選取唯讀核取方塊。

public bool ReadOnlyChecked { get; set; }

屬性值

如果選取唯讀核取方塊,則為 true,否則為 false。 預設值是 false

範例

下列程式碼範例示範 如何使用 ReadOnlyChecked 屬性。 本範例會顯示 OpenFileDialog 屬性設定為 trueShowReadOnly 方塊。 如果使用者按一下選項以唯讀模式開啟檔案, ReadOnlyChecked 則 屬性會評估為 true ,並使用 OpenFile 方法來開啟檔案。 否則,類別 FileStream 會用來以讀取/寫入模式開啟檔案。

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)
            {
                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;
}

備註

狀態 ReadOnlyChecked 不會影響用來開啟對話方塊中所選取檔案的讀取/寫入模式 OpenFileDialog.OpenFileOpenFile 一律會以唯讀模式開啟檔案。

必須先 ShowReadOnly 設定 屬性,才能在對話方塊中顯示唯讀核取方塊。

適用於

產品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

另請參閱