使用正则表达式引用一组文件路径

时间:2013-07-18 20:38:15

标签: c# asp.net regex file-io

我可以使用正则表达式对磁盘进行一次读取,而不是这样做三次吗?

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));

6 个答案:

答案 0 :(得分:4)

假设你不在乎你打的是什么:

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;
    }
}

如果path不是null,则找到一个。

答案 1 :(得分:2)

尝试这样的事情(仅适用于.NET4及之后):

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 :(得分:1)

您可以使用Directory.EnumerateFiles获取目录位置中所有文件的列表并使用LINQ查找路径:

var files = Directory.EnumerateFiles(Server.MapPath("~/Assets/Images/"));

if(files.Contains(string.Format("{0}.png", id))
{
}

根据文件的数量,可能产生比解决方案更好的结果。

答案 3 :(得分:0)

这对@ Oded的想法不仅仅是其他任何修改,但我可能会这样做:

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();

答案 4 :(得分:0)

这对你有用吗?它使用正则表达式并保持匹配的优先级

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;

如果我稍微缩小它(2个陈述)

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;

答案 5 :(得分:0)

很多很好的答案,但我采用了这种方法,结合了一对。

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;
}