Software & Finance

C# - Open File Dialog

OpenFileDialog is a class in C# defined under System.Windows.Forms that provides easy way to invoke file dialog and browse the file information. This is very similar to CFileDialog class in MFC.



Source Code

private void openToolStripMenuItem1_Click(object sender, EventArgs e)


        OpenFileDialog ofd = new OpenFileDialog();

        ofd.InitialDirectory = "c:\\";

        ofd.Filter = "jpeg files (*.jpg)|*.jpg|" +

                         "gif files (*.gif)|*.gif|" +

                         "tiff files (*.tiff)|*.tiff|" +

                         "bmp files (*.bmp)|*.bmp|" +

                         "png files (*.png)|*.png|" +

                         "All files (*.*)|*.*";


      ofd.FilterIndex = 1;

      ofd.RestoreDirectory = true;


      string fname = ofd.FileName;





Open the file dialog with the predefined the filter information.