7

我可以用正则表达式读一次磁盘而不是读三遍吗?

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

6 回答 6

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你找到了一个。

于 2013-07-18T20:44:19.370 回答
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)));
于 2013-07-18T20:51:43.450 回答
1

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

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

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

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

于 2013-07-18T20:44:11.023 回答
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;
于 2013-07-18T21:04:08.480 回答
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;
}
于 2013-07-18T21:44:17.520 回答
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();
于 2013-07-18T20:51:25.257 回答