C# - 从远程目录中获取文件名(没有锚点)

时间:2014-07-14 19:25:24

标签: c# html

我一直试图从我们的localhost / remote目录列表中获取该文件夹中的所有图像。

我到目前为止所尝试的内容(基于其他SO的问题,但稍微改变一下,将其包括在我的arr'中,但仍使用WebRequest。):

Regex regex = new Regex(GetDirectoryListingRegexForUrl(url));
var arr = regex.Matches(photosName).Cast<Match>().Select(m => m.Value).ToArray();

public static string GetDirectoryListingRegexForUrl(string url)
{
    if (url.Equals("http://localhost:MyPort/MyNewbieProject/images/"))
    {
        return "<a href=\".*\">(?<name>.*)</a>";
    }
    throw new EverythingExplodedException();
}

因此,结果就是这样:

"<a href=\"All-Your-Base-00.jpg\"> All-Your-Base-00.jpg</a>"

我想获得“All-Your-Base-00.jpg”,所以我可以将它下载到我的应用程序中并用它来填充我的图像。

我该怎么办?我应该手动修剪这个字符串,还是有更好的方法呢?

1 个答案:

答案 0 :(得分:2)

您希望从返回的匹配中获取捕获的组。也就是说,你有:

var arr = regex.Matches(photosName).Cast<Match>().Select(m => m.Value).ToArray();

首先,将其更改为:

var arr = regex.Matches(photosName).Cast<Match>();

然后你想要经历每场比赛:

foreach (Match m in arr)
{
    var name = m.Groups["name"].Value;
    // name now contains the captured group
}
相关问题