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), 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.
The example assumes your form has a Button control and an OpenFileDialog component.
' Visual Basic
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
Dim sr As New System.IO.StreamReader(OpenFileDialog1.FileName)
MessageBox.Show(sr.ReadToEnd)
sr.Close()
End If
End Sub
// C#
private void button1_Click(object sender, System.EventArgs e)
{
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
System.IO.StreamReader sr = new
System.IO.StreamReader(openFileDialog1.FileName);
MessageBox.Show(sr.ReadToEnd());
sr.Close();
}
}
// C++
private:
System::Void button1_Click(System::Object * sender,
System::EventArgs * e)
{
if(openFileDialog1->ShowDialog() == DialogResult::OK)
{
System::IO::StreamReader * sr = new
System::IO::StreamReader(openFileDialog1->FileName);
MessageBox::Show(sr->ReadToEnd());
sr->Close();
}
}
Visual C# and Visual C++ Note Be sure that the necessary code to enable the event handler is present. In this case, it would be similar to the following:
this.button1.Click += new System.EventHandler(this.button1_Click);
this->button1->Click += new
System::EventHandler(this, button1_Click);
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.
The example assumes your form has a Button control.
' Visual Basic
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() = 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
// C#
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() == DialogResult.OK)
{
// Assign the cursor in the Stream to the Form's Cursor property.
this.Cursor = new Cursor(openFileDialog1.OpenFile());
}
}
// C++
private:
System::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() == DialogResult::OK)
{
// Assign the cursor in the Stream to
// the Form's Cursor property.
this->Cursor = new
System::Windows::Forms::Cursor(
openFileDialog1->OpenFile());
}
}
Visual C# and Visual C++ Note Be sure that the necessary code to enable the event handler is present. In this case, it would be similar to the following:
this.button1.Click += new System.EventHandler(this.button1_Click);
this->button1->Click += new
System::EventHandler(this, button1_Click);
See Also
OpenFileDialog Component (Windows Forms) | OpenFileDialog Class