How to: Open Files Using the OpenFileDialog Component
The OpenFileDialog component allows users to browse the folders of their computer or any computer on the network and select one or more files to open. The dialog box returns the path and name of the file the user selected in the dialog box.
Once the user has selected the file to be opened, there are two approaches to the mechanism of opening the file. If you prefer to work with file streams, you can create an instance of the StreamReader class. Alternately, you can use the OpenFile method to open the selected file.
The first example below involves a FileIOPermission permission check (as described in the "Security Note" below), but gives you access to the filename. You can use this technique from the Local Machine, Intranet, and Internet zones. The second method also does a FileIOPermission permission check, but is better suited for applications in the Intranet or Internet zones.
To open a file as a stream using the OpenFileDialog component
Display the Open File dialog box and call a method to open the file selected by the user.
One approach is to use the ShowDialog method to display the Open File dialog box, and use an instance of the StreamReader class to open the file.
The example below uses the Button control's Click event handler to open an instance of the OpenFileDialog component. When a file is chosen and the user clicks OK, the file selected in the dialog box opens. In this case, the contents are displayed in a message box, just to show that the file stream has been read.
Security Note To get or set the FileName property, your assembly requires a privilege level granted by the System.Security.Permissions.FileIOPermission class. If you are running in a partial-trust context, the process might throw an exception due to insufficient privileges. For more information, see Code Access Security Basics.
The example assumes your form has a Button control and an OpenFileDialog component.
Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then Dim sr As New System.IO.StreamReader(OpenFileDialog1.FileName) MessageBox.Show(sr.ReadToEnd) sr.Close() End If End Sub
private void button1_Click(object sender, System.EventArgs e) { if(openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { System.IO.StreamReader sr = new System.IO.StreamReader(openFileDialog1.FileName); MessageBox.Show(sr.ReadToEnd()); sr.Close(); } }
private: void button1_Click(System::Object ^ sender, System::EventArgs ^ e) { if(openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK) { System::IO::StreamReader ^ sr = gcnew System::IO::StreamReader(openFileDialog1->FileName); MessageBox::Show(sr->ReadToEnd()); sr->Close(); } }
(Visual C# and Visual C++) Place the following code in the form's constructor to register the event handler.
this.button1.Click += new System.EventHandler(this.button1_Click);
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
Note
For more information about reading from file streams, see FileStream.BeginRead Method and FileStream.Read Method.
To open a file as a file using the OpenFileDialog component
Use the ShowDialog method to display the dialog box and the OpenFile method to open the file.
The OpenFileDialog component's OpenFile method returns the bytes that compose the file. These bytes give you a stream to read from. In the example below, an OpenFileDialog component is instantiated with a "cursor" filter on it, allowing the user to choose only files with the file name extension .cur. If a .cur file is chosen, the form's cursor is set to the selected cursor.
Security Note To call the OpenFile method, your assembly requires a privilege level granted by the System.Security.Permissions.FileIOPermission class. If you are running in a partial-trust context, the process might throw an exception due to insufficient privileges. For more information, see Code Access Security Basics.
The example assumes your form has a Button control.
Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click ' Displays an OpenFileDialog so the user can select a Cursor. Dim openFileDialog1 As New OpenFileDialog() openFileDialog1.Filter = "Cursor Files|*.cur" openFileDialog1.Title = "Select a Cursor File" ' Show the Dialog. ' If the user clicked OK in the dialog and ' a .CUR file was selected, open it. If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then ' Assign the cursor in the Stream to the Form's Cursor property. Me.Cursor = New Cursor(openFileDialog1.OpenFile()) End If End Sub
private void button1_Click(object sender, System.EventArgs e) { // Displays an OpenFileDialog so the user can select a Cursor. OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = "Cursor Files|*.cur"; openFileDialog1.Title = "Select a Cursor File"; // Show the Dialog. // If the user clicked OK in the dialog and // a .CUR file was selected, open it. if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { // Assign the cursor in the Stream to the Form's Cursor property. this.Cursor = new Cursor(openFileDialog1.OpenFile()); } }
private: void button1_Click(System::Object ^ sender, System::EventArgs ^ e) { // Displays an OpenFileDialog so the user can select a Cursor. OpenFileDialog ^ openFileDialog1 = new OpenFileDialog(); openFileDialog1->Filter = "Cursor Files|*.cur"; openFileDialog1->Title = "Select a Cursor File"; // Show the Dialog. // If the user clicked OK in the dialog and // a .CUR file was selected, open it. if (openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK) { // Assign the cursor in the Stream to // the Form's Cursor property. this->Cursor = gcnew System::Windows::Forms::Cursor( openFileDialog1->OpenFile()); } }
(Visual C# and Visual C++) Place the following code in the form's constructor to register the event handler.
this.button1.Click += new System.EventHandler(this.button1_Click);
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);