OpenFileDialog.OpenFile Method
Opens the file selected by the user, with read-only permission. The file is specified by the FileName property.
[Visual Basic] Public Function OpenFile() As Stream [C#] public Stream OpenFile(); [C++] public: Stream* OpenFile(); [JScript] public function OpenFile() : Stream;
Return Value
A Stream that specifies the read-only file selected by the user.
Exceptions
| Exception Type | Condition |
|---|---|
| ArgumentNullException | The file name is a null reference (Nothing in Visual Basic). |
Remarks
The OpenFile method is used to provide a facility to quickly open a file from the dialog box. The file is opened in read-only mode for security purposes. To open a file in read/write mode, you must use another method such as FileStream.
Example
[Visual Basic, C#, C++] The following example demonstrates how to use the OpenFile method. This example display an OpenFileDialog 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.
[Visual Basic] Private Function OpenFile() As FileStream ' Displays an OpenFileDialog and shows the read/only files. Dim DlgOpenFile As New OpenFileDialog() DlgOpenFile.ShowReadOnly = True Dim Fs As FileStream If DlgOpenFile.ShowDialog() = DialogResult.OK Then ' If ReadOnlyChecked is true, uses the OpenFile method to ' open the file with read/only access. If DlgOpenFile.ReadOnlyChecked = True Then Return DlgOpenFile.OpenFile() ' Otherwise, opens the file with read/write access. Else Dim Path As String = DlgOpenFile.FileName Return New FileStream(Path, System.IO.FileMode.Open, _ System.IO.FileAccess.ReadWrite) End If End If End Function [C#] 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. if(dlgOpenFile.ReadOnlyChecked == true) { return (FileStream)dlgOpenFile.OpenFile(); } // Otherwise, opens the file with read/write access. else { string path = dlgOpenFile.FileName; return new FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite); } } return null; } [C++] 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. if (dlgOpenFile->ReadOnlyChecked == true) { return dynamic_cast<FileStream*>(dlgOpenFile->OpenFile()); } // Otherwise, opens the file with read/write access. else { String* path = dlgOpenFile->FileName; return new FileStream(path, System::IO::FileMode::Open, System::IO::FileAccess::ReadWrite); } } return 0; }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family
.NET Framework Security:
- FileDialogPermission to open a file. Associated enumeration: FileDialogPermissionAccess.Open.
See Also
OpenFileDialog Class | OpenFileDialog Members | System.Windows.Forms Namespace | Stream