Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用正则表达式引用一组文件路径_C#_Asp.net_Regex_File Io - Fatal编程技术网

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

C# 使用正则表达式引用一组文件路径,c#,asp.net,regex,file-io,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

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

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

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


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

假设您不在乎点击哪个:

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

这是对@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();
var extensions=newhashset(StringComparer.OrdinalIgnoreCase)
{.png“,.gif“,.jpg“,.bmp”};
var firstMatchingPath=Directory.EnumerateFiles(Server.MapPath(“~/Assets/Images/”)
.Where(s=>extensions.Contains(Path.GetExtension)
.FirstOrDefault();
尝试类似的方法(仅适用于.NET4及以后版本):


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

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

对于这种方法,我会得到“路径中的非法字符”。这会告诉您是否存在其中一个文件,但不会告诉您是哪一个。很明显,OP想要现有文件的路径。您想要这样做的原因是什么?性能?
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;
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;
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;
}