Get File To Open using file dialog - CSharp System.Windows.Forms

CSharp examples for System.Windows.Forms:Dialog

Description

Get File To Open using file dialog

Demo Code


using System.IO ;
using System.Windows.Forms ;
using System;//from  w w  w.ja  v  a 2  s .c  om

public class Main{
 
      public static string GetFileToOpen(string title ,string extendName ,string iniDir)
      {
         OpenFileDialog openDlg = new OpenFileDialog();
         openDlg.Filter  = string.Format("The Files (*{0})|*{0}" ,extendName);
         openDlg.FileName = "" ;
            openDlg.InitialDirectory = iniDir;
         if(title != null)
         {
            openDlg.Title = title ;
         }

         openDlg.CheckFileExists = true;
         openDlg.CheckPathExists = true;

         DialogResult res =openDlg.ShowDialog ();
         if(res == DialogResult.OK)
         {
            return openDlg.FileName ;
         }
         
         return null ;
      }
    
      public static string GetFileToOpen(string title)
      {
         OpenFileDialog openDlg = new OpenFileDialog();
         openDlg.Filter  = "All Files (*.*)|*.*";
         openDlg.FileName = "" ;      
         if(title != null)
         {
            openDlg.Title = title ;
         }

         openDlg.CheckFileExists = true;
         openDlg.CheckPathExists = true;

         DialogResult res =openDlg.ShowDialog ();
         if(res == DialogResult.OK)
         {
            return openDlg.FileName ;
         }
         
         return null ;
      }
}

Related Tutorials