Wednesday, February 8, 2012

C# Open File Dialog (OpenFileDialog)



/***********************************************************************
/* Description: Description : Use the following c# method to get the file name with the full path with the help of
/* dialog box.
/* See the example bellow, how to use it. Code compiled with C# VS2010
***********************************************************************/




public string FileNameFromOpenDialog(string fileExtDescription, string fileExtFilter)
{
System.Windows.Forms.OpenFileDialog openFD = new System.Windows.Forms.OpenFileDialog();

openFD.Filter = fileExtDescription + "(*." + fileExtFilter + ")|*." + fileExtFilter;
openFD.RestoreDirectory = true;

if (openFD.ShowDialog() != System.Windows.Forms.DialogResult.OK)
{
return null;
}
return openFD.FileName;
}

/****** How to use:****************************************************************************************/

string fileName = FileNameFromOpenDialog("Data Files", "dat");