2009-04-21 152 views
0

C#2008c#使用Web客戶端在服務器上檢查文件

我已經使用WebClient DownloadFile方法。

我可以下載我想要的文件。但是,客戶堅持要創建包含版本號的不同文件夾。所以文件夾的名稱應該是這樣的:1.0.1,1.0.2,1.0.3等。

所以文件將包含在最新版本的文件夾1.0.3中。但是,我的Web客戶端如何檢測哪一個是最新的?

客戶端會在啓動時檢查它。除非我真的下載所有的文件夾,然後進行比較。我不知道我還能做到這一點。

非常感謝您的任何建議,

回答

3

創建一個頁面,爲您提供當前版本號。

string versionNumber = WebClient.DownloadString(); 
1

This question可能會爲您提供一些有用的信息。請閱讀我的答案,它涉及在遠程服務器上枚舉文件。

2

Allow directory browsing in 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; 
    } 
} 
+0

你好,感謝您的源代碼。我想我可以下載整個目錄。然而,每個人都可能變得越來越大,而且我有很多,這可能是昂貴的。那麼下載可能需要很長時間。我在那裏跳轉會有一些方法來排序遠程服務器上的文件夾。而不是下載整個目錄。 – ant2009 2009-04-21 17:00:34