Link to a set of file paths using a regular expression

Is it possible to do one read on a disk with a regular expression instead of doing this three times?

var path = HttpContext.Current.Server.MapPath(string.Format("~/Assets/Images/{0}.png", id)); if (!File.Exists(path)) { path = HttpContext.Current.Server.MapPath(string.Format("~/Assets/Images/{0}.jpg", id)); if (!File.Exists(path)) { path = HttpContext.Current.Server.MapPath(string.Format("~/Assets/Images/{0}.gif", id)); 
+7
c # regex file-io
source share
6 answers

Assuming you don't care what you click:

 using System.IO string imagesPath = HttpContext.Current.Server.MapPath("~/Assets/Images"); string path = null; foreach (var filePath in Directory.GetFiles(imagesPath, id + ".*")) { switch (Path.GetExtension(filePath)) { case ".png": case ".jpg": case ".gif": path = filePath; break; } } 

If path not null , you found it.

+4
source share

Try something like this (only works in .NET4 and after):

 string folder = HttpContext.Current.Server.MapPath("~/Assets/Images/"); string[] extensions = { ".jpg", ".gif", ".png" }; bool exists = Directory.EnumerateFiles(folder, "*.*", SearchOption.TopDirectoryOnly) .Any(f => extensions.Contains(Path.GetExtension(f))); 
+2
source share

You can get a list of all the files in a directory and use LINQ to find the path using Directory.EnumerateFiles :

 var files = Directory.EnumerateFiles(Server.MapPath("~/Assets/Images/")); if(files.Contains(string.Format("{0}.png", id)) { } 

Depending on the number of files, this may lead to better results than your solution.

+1
source share

This is more of a modification to the @Oded idea than anything else, but I'd rather do something like this:

 var extensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { ".png", ".gif", ".jpg", ".bmp" }; var firstMatchingPath = Directory.EnumerateFiles(Server.MapPath("~/Assets/Images/")) .Where(s => extensions.Contains(Path.GetExtension(s)) .FirstOrDefault(); 
0
source share

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) // Taking advantage of png jpg gif is reverse alphabetical order, // take .OrderByDecending() out if you don't care which one gets found first .FirstOrDefault(); var path = fileInfo != default(FileInfo) ? fileInfo.FullName : string.Empty; 

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; 
0
source share

A lot of good answers, but I went with an approach that combines a couple.

 var path = Directory.EnumerateFiles(HttpContext.Current.Server.MapPath("~/Assets/Images/"), string.Format("{0}.*", id), SearchOption.TopDirectoryOnly).FirstOrDefault(); switch (Path.GetExtension(path)) { case ".png": case ".jpg": case ".gif": break; default: return; } 
0
source share

All Articles