ant2009
ant2009

Reputation: 22486

c# check files on server using web client

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,

Upvotes: 0

Views: 7367

Answers (3)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038820

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;
    }
}

Upvotes: 2

Cerebrus
Cerebrus

Reputation: 25775

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

Upvotes: 1

sshow
sshow

Reputation: 9094

Create a page which gives you the current version number.

string versionNumber = WebClient.DownloadString();

Upvotes: 3

Related Questions