0

C# 2008

我使用了 WebClient DownloadFile 方法。

我可以下载我想要的文件。但是,客户坚持创建包含版本号的不同文件夹。所以文件夹的名称应该是这样的:1.0.1、1.0.2、1.0.3 等。

因此,这些文件将包含在本案例文件夹 1.0.3 中的最新版本中。但是,我的 Web 客户端如何检测哪个是最新的?

客户端将在启动时检查此内容。除非我实际下载所有文件夹然后进行比较。我不确定我还能如何做到这一点。

非常感谢您的任何建议,

4

3 回答 3

3

创建一个为您提供当前版本号的页面。

string versionNumber = WebClient.DownloadString();
于 2009-04-21T10:22:31.490 回答
2

允许在 IIS 中浏览目录并下载根文件夹。然后你可以找到最新的版本号并构造实际的 url 来下载。这是一个示例(假设您的目录格式为 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;
    }
}
于 2009-04-21T12:00:59.727 回答
1

这个问题可能对你有一些有用的信息。请阅读我的答案,该答案涉及远程服务器上的枚举文件。

于 2009-04-21T10:35:59.167 回答