tags:

views:

1049

answers:

3

Hello,

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,

+3  A: 

Create a page which gives you the current version number.

string versionNumber = WebClient.DownloadString();
sshow
+1  A: 

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

Cerebrus
+2  A: 

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;
    }
}
Darin Dimitrov
Hello, Thanks for the source code. I guess I could download the entire directory. However, it could be costly each each one could grow to become big and it I have many. Then it might take too much time to download. I was hopping there would be a some way to sort the folders on the remote server. Rather than downloading the entire directory.
robUK