OpenFileDialog::OpenFile Method
Opens the file selected by the user, with read-only permission. The file is specified by the FileName property.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
| Exception | Condition |
|---|---|
| ArgumentNullException | The file name is nullptr. |
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.
The following code example demonstrates how to use the OpenFile method.
private: void button1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ ) { Stream^ myStream; OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog; openFileDialog1->InitialDirectory = "c:\\"; openFileDialog1->Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; openFileDialog1->FilterIndex = 2; openFileDialog1->RestoreDirectory = true; if ( openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK ) { if ( (myStream = openFileDialog1->OpenFile()) != nullptr ) { // Insert code to read the stream here. myStream->Close(); } } }
- FileDialogPermission
to open a file. Associated enumeration: FileDialogPermissionAccess::Open.
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.