2014-01-28 45 views
0

我們正在進行軟件構建並將它們存儲在預發佈文件夾中的文件夾中,格式爲四位數的Major.Minor.Build.rev(0.0.0.0)格式。如何在最新版本文件夾的LINQ查詢中使用版本類

  1. 我想知道是否有方法在LINQ查詢中使用版本類?
  2. 使用LINQ查詢會有什麼好處,還是應該堅持使用非LINQ查詢代碼?

下面是代碼我目前:

static void Main(string[] args) 
{ 
    string latest = GetLatestBuildVersion(@"\\path\to\network\folder"); 
    latest = ""; 
} 

static string GetLatestBuildVersion(string sPath) 
{ 
    Version latestVersion = new Version(); 
    string[] dirs = Directory.GetDirectories(sPath); 
    foreach (string dir in dirs) 
    { 
     string subfolderNameOnly = Path.GetFileName(dir); 
     try 
     { 
      Version subfolderVersion = new Version(subfolderNameOnly); 
      if (latestVersion.CompareTo(subfolderVersion) < 0) 
      { 
       latestVersion = subfolderVersion; 
      } 
     } 
     catch (Exception ex) 
     { 
      // Just continue 
     } 
    } 
    return latestVersion.ToString(); 
} 
+0

如果你已經有可用的代碼,它看起來像喲你應該堅持下去。任何獲得的優勢都會很小。 – Chris

+0

它確實有效。我希望用更少的代碼來完成。我有一些代碼可以在這裏查詢,但是我被困在Path.GetFileName和subfolderversion的版本類的第二個實例化中。 –

回答

0

這是鏈LINQ方法完美的任務:

static string GetLatestBuildVersion(string sPath) 
{ 
    return Directory.GetDirectories(sPath) 
     .Select(v => new Version(Path.GetFileName(v))) 
     .OrderBy(v => v) 
     .Last(); 
} 

添加錯誤需要處理(給出的特定背景下,我'而是讓它炸燬,比它運行的文件不是我所期望的那些文件)

+0

非常酷!謝謝Alex! –

相關問題