Here I’m going to demonstrate the use of OpenFileDialog in C#.

 Example

 
string strFileName=string.Empty;                       
//creationg object of SaveFileDialog
OpenFileDialog openFileDialog1= new OpenFileDialog();
//setting filter of open file dialog box
  openFileDialog1.Filter= "Text file|*.txt";
//checking whether open button is clicked or not
   if (openFileDialog1.ShowDialog()== DialogResult.OK)
     {
//storing filename opened through openfiledialog box.
      strFileName = openFileDialog1.FileName;
//creating reader to read open file.
     System.IO.StreamReader sr = new System.IO.StreamReader(openFileDialog1.FileName);
//displaying file contents in message box.
      MessageBox.Show(sr.ReadToEnd());
//closing stream reader.
                sr.Close();
    }
 

 


 

  Modified On Nov-29-2017 03:09:16 PM

Leave Comment