How to get file extension from OpenFileDialog?

M.Azad picture M.Azad · Mar 26, 2012 · Viewed 53.4k times · Source

I want just get Image(.JPG,.PNG,.Gif) File from my OpenFileDialog How can I get file extension from OpenFileDialog?

Is it impossible?

Answer

Steve picture Steve · Mar 26, 2012

To filter only certain types of file use Filter Property

OpenFileDialog1.Filter = "Image Files (JPG,PNG,GIF)|*.JPG;*.PNG;*.GIF";

To get the file extension use the Path helper GetFileExtension

if (OpenFileDialog1.ShowDialog() == DialogResult.OK)
   string ext = Path.GetExtension(OpenFileDialog1.FileName);