You can make use of regular expression for simplicity of code // the regex for an image Regex imageFilenameRegex = new Regex(@"(.*?)\.(jpg|jpeg|png|gif)$"); if (imageFilenameRegex.IsMatch("Your FileName or Path here",RegexOptions.IgnoreCase)) { // we have a valid filename // .. do something .. }