2

如何从 Web 目录中获取文件列表?如果我访问 Web 目录 URL,则 Internet 浏览器会列出该目录中的所有文件。现在我只想在 C# 中获取该列表并在 BITS (Background Intelligent Transfer Service) 中下载它们。

4

5 回答 5

4

关于“在 C# 中获取该列表”部分:

foreach (string filename in 
    Directory.GetFiles(
        Server.MapPath("/"), "*.jpg", 
        SearchOption.AllDirectories))
{
    Response.Write(
        String.Format("{0}<br />", 
            Server.HtmlEncode(filename)));
}
于 2009-10-20T00:48:28.393 回答
0
private void ListFiles()
{

    //get the user calling this page 
    Gaf.Bl.User userObj = base.User;
    //get he debug directory of this user
    string strDebugDir = userObj.UserSettings.DebugDir;
    //construct the Directory Info directory 
    DirectoryInfo di = new DirectoryInfo(strDebugDir);
    if (di.Exists == true)
    {

        //get the array of files for this 
        FileInfo[] rgFiles = di.GetFiles("*.html");
        //create the list ... .it is easier to sort ... 
        List<FileInfo> listFileInfo = new List<FileInfo>(rgFiles);
        //inline sort descending by file's full path 
        listFileInfo.Sort((x, y) => string.Compare(y.FullName, x.FullName));
        //now print the result 
        foreach (FileInfo fi in listFileInfo)
        {
            Response.Write("<br><a href=" + fi.Name + ">" + fi.Name + "</a>");
        } //eof foreach
    } //eof if dir exists

} //eof method 
于 2010-05-02T17:55:02.247 回答
0

好吧,如果 Web 服务器允许列出有问题的目录中的文件,那么您就可以开始了。

不幸的是,对于 Web 服务器应该如何返回列表没有标准。它通常采用 HTML 格式,但 HTML 在多个 Web 服务器中的格式并不总是相同。

如果您想始终从同一 Web 服务器上的同一目录下载文件,只需在 Web 浏览器的目录中执行“查看源代码”即可。然后尝试编写一个小的正则表达式,它将从 HTML 源中获取每个文件名。

然后您可以创建一个 WebClient,请求目录 URL,解析响应以使用您的正则表达式获取文件名,然后使用您的 BITS 客户端处理文件

希望这可以帮助

于 2009-10-25T17:45:44.910 回答
0

这是我最近调查的一个有趣的话题。如您所知,您可以通过 COM 访问 BITS,但这里有几个项目可以使其更容易:

SharpBITS.NET
Forms Designer 友好的后台智能传输服务 (BITS) 包装器

MSDN 上的这篇文章可能比您想知道的要多一些。

我对 CodeProject 链接中的代码进行了试验,它似乎运行良好。CodePlex 项目看起来非常好,但我还没有尝试过。

于 2009-10-19T23:56:11.807 回答
0

我编写了一些代码,可以从允许列表目录的 IIS 站点获取所有路径信息,包括文件和目录。您可以自定义正则表达式以满足您的需要(或更改为使用 html 解析器)。此外,您可以自己添加一些代码以获取更详细的信息,例如文件大小或创建时间。

您可以在 2 行中获取所有路径信息:

List<PathInfo> pathInfos = new List<PathInfo>();
HttpHelper.GetAllFilePathAndSubDirectory("http://localhost:33333/", pathInfos);

辅助代码:

public static class HttpHelper
{
    public static string ReadHtmlContentFromUrl(string url)
    {
        string html = string.Empty;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        using (Stream stream = response.GetResponseStream())
        using (StreamReader reader = new StreamReader(stream))
        {
            html = reader.ReadToEnd();
        }
        //Console.WriteLine(html);
        return html;
    }

    public static void GetAllFilePathAndSubDirectory(string baseUrl, List<PathInfo> pathInfos)
    {
        Uri baseUri = new Uri( baseUrl.TrimEnd('/') );
        string rootUrl = baseUri.GetLeftPart(UriPartial.Authority);


        Regex regexFile = new Regex("[0-9] <a href=\"(http:|https:)?(?<file>.*?)\"", RegexOptions.IgnoreCase);
        Regex regexDir = new Regex("dir.*?<a href=\"(http:|https:)?(?<dir>.*?)\"", RegexOptions.IgnoreCase);

        string html = ReadHtmlContentFromUrl(baseUrl);
        //Files
        MatchCollection matchesFile = regexFile.Matches(html);
        if (matchesFile.Count != 0)
            foreach (Match match in matchesFile)
                if (match.Success)
                    pathInfos.Add(
                        new PathInfo( rootUrl + match.Groups["file"], false));
        //Dir
        MatchCollection matchesDir = regexDir.Matches(html);
        if (matchesDir.Count != 0)
            foreach (Match match in matchesDir)
                if (match.Success)
                {
                    var dirInfo = new PathInfo(rootUrl + match.Groups["dir"], true);
                    GetAllFilePathAndSubDirectory(dirInfo.AbsoluteUrlStr, dirInfo.Childs);
                    pathInfos.Add(dirInfo);
                }                        

    }


    public static void PrintAllPathInfo(List<PathInfo> pathInfos)
    {
        pathInfos.ForEach(f =>
        {
            Console.WriteLine(f.AbsoluteUrlStr);
            PrintAllPathInfo(f.Childs);
        });
    }

}



public class PathInfo
{
    public PathInfo(string absoluteUri, bool isDir)
    {
        AbsoluteUrl = new Uri(absoluteUri);
        IsDir = isDir;
        Childs = new List<PathInfo>();
    }

    public Uri AbsoluteUrl { get; set; }

    public string AbsoluteUrlStr
    {
        get { return AbsoluteUrl.ToString(); }
    }

    public string RootUrl
    {
        get { return AbsoluteUrl.GetLeftPart(UriPartial.Authority); }
    }

    public string RelativeUrl
    {
        get { return AbsoluteUrl.PathAndQuery; }
    }

    public string Query
    {
        get { return AbsoluteUrl.Query; }
    }

    public bool IsDir { get; set; }
    public List<PathInfo> Childs { get; set; }


    public override string ToString()
    {
        return String.Format("{0} IsDir {1} ChildCount {2} AbsUrl {3}", RelativeUrl, IsDir, Childs.Count, AbsoluteUrlStr);
    }
}
于 2018-06-26T07:15:53.573 回答