OpenFileDialog.ShowReadOnly Свойство
Определение
Важно!
Некоторые сведения относятся к предварительной версии продукта, в которую до выпуска могут быть внесены существенные изменения. Майкрософт не предоставляет никаких гарантий, явных или подразумеваемых, относительно приведенных здесь сведений.
Возвращает или задает значение, указывающее, содержит ли диалоговое окно флажок только для чтения.
public:
property bool ShowReadOnly { bool get(); void set(bool value); };
public bool ShowReadOnly { get; set; }
member this.ShowReadOnly : bool with get, set
Public Property ShowReadOnly As Boolean
Значение свойства
true Значение , если диалоговое окно содержит флажок только для чтения; falseв противном случае . Значение по умолчанию — false.
Примеры
В следующем примере кода показано использование ShowReadOnly свойства. В этом примере отображается OpenFileDialog поле со свойством, равным ShowReadOnlytrue. Если пользователь щелкает параметр, чтобы открыть файл в режиме только для чтения, 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 )
{
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)
{
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