File Open Dialog box in C#

To Open or to Save file we use dialog box in all the windows applications. To use open or save dialog box we should use the following directive statement.

using System.Windows.Forms;  

Because these dialog boxes are defined in System.windows.Forms. 

In the following code i have used the file open dialog box for demo.

OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Title = "Kanbal.com open dialog box demo";
openFileDialog.InitialDirectory =
@"c:\Program Files";
openFileDialog.Filter =
"All files (*.*)|*.*|All files (*.*)|*.*";
openFileDialog.FilterIndex = 2;
openFileDialog.RestoreDirectory =
true;

if (openFileDialog.ShowDialog() == DialogResult.OK)
{
    txtFileName.Text = openFileDialog.FileName;
}
 
 
 

The above code will display the open dialog box with all files filter like exactly below. 
 

open-dialog-box


You can use various properties like Title, Filter, InitialDirectory to make your dialog box more friendly.



» File Open Dialog box in CSharp


Similar articles

» File Open Dialog box in CSharp

File Open Dialog box in C#

To Open or to Save file we use dialog box in all the windows applications. To use open or save dialog box we should use the following directive statement.

using System.Windows.Forms;

Because these dialog boxes are defined in System.windows.Forms.

In the following code i have used the file open dialog box for demo.

OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Title = "Kanbal.com open dialog box demo";
openFileDialog.InitialDirectory =
@"c:\Program Files";
openFileDialog.Filter =
"All files (*.*)|*.*|All files (*.*)|*.*";
openFileDialog.FilterIndex = 2;
openFileDialog.RestoreDirectory =
true;

if (openFileDialog.ShowDialog() == DialogResult.OK)
{
txtFileName.Text = openFileDialog.FileName;
}

The above code will display the open dialog box with all files filter like exactly below.

open-dialog-box


You can use various properties like Title, Filter, InitialDirectory to make your dialog box more friendly.