简体   繁体   English

C#-从远程目录获取文件名(不带锚)

[英]C# - Get file names from remote directory (without anchors)

I have been trying to get from our localhost/remote directory listing of all images on that folder. 我一直在尝试从我们的本地主机/远程目录中获取该文件夹上所有图像的列表。

What I so far tried follows (based on others SO's questions, but changed a bit to include these on my arr', but still using a WebRequest.): 到目前为止,我尝试了以下操作(基于其他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();
}

So, as a result from that, I get something like: 因此,从中得到的结果是:

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

I wanted to get just the "All-Your-Base-00.jpg", so I could download it into my App and use it to fill my Image's. 我只想获取“ All-Your-Base-00.jpg”,因此可以将其下载到我的应用程序中,并使用它来填充图像。

How should I proceed? 我应该如何进行? Should I manually trim this string, or there is a better way to do so? 我应该手动修剪此字符串,还是有更好的方法?

You want to get the captured group from the returned match. 您要从返回的匹配项中获取捕获的组。 That is, you have: 也就是说,您有:

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

First, change that to: 首先,将其更改为:

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

You then want to go through each match: 然后,您要经历每场比赛:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM