Browse or Open a File

The OpenFileDialog class is defined in Windows.Forms class. You need to add to reference to System.Windows.Form.dll library and call using System.Windows.Forms before using the class.

The OpenFileDialog class can be used to open a file similar to CFileDialog's Open method in VC++. This class is derived from FileDialog. OpenFile method of this class opens a file which can be read by steam.

In this sample code, I use OpenFileDialog class to browse a file.

  1. OpenFileDialog fdlg = new OpenFileDialog();  
  2. fdlg.Title = "C# Corner Open File Dialog" ;  
  3. fdlg.InitialDirectory = @"c:\" ;  
  4. fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*" ;  
  5. fdlg.FilterIndex = 2 ;  
  6. fdlg.RestoreDirectory = true ;  
  7. if(fdlg.ShowDialog() == DialogResult.OK)  
  8. {  
  9.     textBox1.Text = fdlg.FileName ;  
  10. }  
Title member let you set the title of the open dialog. Filter member let you set a filter for types of files to open.


FileName member gives you the name of the selected file.
 
Read more >> OpenFileDialog In C# 


Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.