openFileDialog.FileName returns a string representing the full path of the selected file including the extension, since it's a string you can use the static String method Contains to check if the file name coantians .foo or .bar, like this:
if (openFileDialog.FileName.Contains(".foo"))
{
MessageBox.Show("You opened a .foo file");
}
else if (openFileDialog.FileName.Contains(".bar"))
{
MessageBox.Show("You opened a .bar file");
} Or use the static GetExtension method of the Path class like this:
if (System.IO.Path.GetExtension(openFileDialog.FileName).ToLower() == ".foo")
{
MessageBox.Show("You opened a .foo file");
}
else if (System.IO.Path.GetExtension(openFileDialog.FileName).ToLower() == ".bar")
{
MessageBox.Show("You opened a .bar file");
} Note: Before this code make sure you set openFileDialog.FileName to an empty string so if you open the openFileDialog twice and you click Cancel it won't execute the code inside the if/else statement again, like this:
openFileDialog.FileName = String.Empty
or
openFileDialog.FileName = "" Maybe there's a better way to check the file extension, I'm not sure, but this how I do it...