c# check files on server using web client

淺唱寂寞╮ 提交于 2019-12-11 15:54:44

问题


C# 2008

I have using the WebClient DownloadFile method.

I can download the files I want. However, the client has insisted on creating different folders which will contain the version number. So the name of the folders would be something like this: 1.0.1, 1.0.2, 1.0.3, etc.

So the files will be contained in the latest version in this case folder 1.0.3. However, how can my web client detect which is the latest one?

The client will check this when it starts up. Unless I actually download all the folders and then compare. I am not sure how else I can do this.

Many thanks for any advice,


回答1:


Create a page which gives you the current version number.

string versionNumber = WebClient.DownloadString();



回答2:


Allow directory browsing in IIS and download the root folder. Then you could find the latest version number and construct the actual url to download. Here's a sample (assuming your directories will be of the form Major.Minor.Revision):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;

class Program
{
    static void Main(string[] args)
    {
        using (var client = new WebClient())
        {
            var directories = client.DownloadString("http://example.com/root");
            var latestVersion = GetVersions(directories).Max();
            if (latestVersion != null)
            {
                // construct url here for latest version
                client.DownloadFile(...);
            }
        }
    }

    static IEnumerable<Version> GetVersions(string directories)
    {
        var regex = new Regex(@"<a href=""[^""]*/([0-9]+\.[0-9]+\.[0-9])+/"">",
            RegexOptions.IgnoreCase);

        foreach (Match match in regex.Matches(directories))
        {
            var href = match.Groups[1].Value;
            yield return new Version(href);
        }
        yield break;
    }
}



回答3:


This question might have some useful information for you. Please read my answer which deals with enumerating files on a remote server.



来源:https://stackoverflow.com/questions/771950/c-sharp-check-files-on-server-using-web-client

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!