Does this work for you? Its use of regex and maintaining the priority of matching
var folderPath = HttpContext.Current.Server.MapPath(string.Format("~/Assets/Images")); var regex = new Regex(string.Format("{0}[.](png|jpg|gif)", id)); var fileInfo = new DirectoryInfo(folderPath) .GetFiles() .Where(w => regex.Match(w.Name).Success) .OrderByDescending(o => o.Extension)
If I squeezed it a little (2 statements)
var fileInfo = new DirectoryInfo(HttpContext.Current.Server.MapPath(string.Format("~/Assets/Images"))) .GetFiles() .Where(w => Regex.Match(w.Name, string.Format("{0}[.](png|jpg|gif)", id)).Success) .OrderByDescending(o => o.Extension) .FirstOrDefault(); var path = fileInfo != default(FileInfo) ? fileInfo.FullName : string.Empty;
Sɥᴉuʇɐɹo Sɐsɐʞᴉ
source share