2016-04-13 9 views
-2

私は内部にupdate10、Update15、Update13という名前の多くのフォルダがあるディレクトリという名前のディレクトリがあります。 フォルダ名の数字を比較してそのフォルダへのパスを返すことで、最新のアップデートを入手する必要があります。 すべてのヘルプはあなたがLINQを使用することができますディレクトリ内の最新の更新を検索するC#

+1

すべてのフォルダは "Update"という単語と1桁以上の数字で始まりますか?そして、より高い数字は最新のものですか? – Steve

+1

作成/変更された日付ははるかに正確であるべきですか? – Soundararajan

+1

あなたはこれを行けると確信しています。あなたが試したことを投稿してください –

答えて

1

をaprecciatedされるだろう:LINQは、このような副作用を引き起こすことはありませんので、

int updateInt = 0; 

var mostRecendUpdate = Directory.EnumerateDirectories(updateDir) 
    .Select(path => new 
    { 
     fullPath = path, 
     directoryName = System.IO.Path.GetFileName(path) // returns f.e. Update15 
    }) 
    .Where(x => x.directoryName.StartsWith("Update")) // precheck 
    .Select(x => new 
    { 
     x.fullPath, x.directoryName, 
     updStr = x.directoryName.Substring("Update".Length) // returns f.e. "15" 
    }) 
    .Where(x => int.TryParse(x.updStr, out updateInt))  // int-check and initialization of updateInt 
    .Select(x => new { x.fullPath, x.directoryName, update = updateInt }) 
    .OrderByDescending(x => x.update)      // main task: sorting 
    .FirstOrDefault();          // return newest update-infos 

if(mostRecendUpdate != null) 
{ 
    string fullPath = mostRecendUpdate.fullPath; 
    int update = mostRecendUpdate.update; 
} 

cleaner versionは、アウトパラメータとしてローカル変数を使用してint?の代わりを返すメソッドを使用しています。彼らは有害かもしれません。

注:現在、クエリでは大文字と小文字が区別されますが、有効なディレクトリとしてUPDATE11は認識されません。あなたは大文字と小文字を区別しない比較したい場合は、適切なStartsWithオーバーロードを使用する必要があります。

..... 
.Where(x => x.directoryName.StartsWith("Update", StringComparison.InvariantCultureIgnoreCase)) // precheck 
..... 
+0

ありがとうございます –

0

最善のアプローチは、質問が示唆にコメントとして変更された日付を使用することです。 しかし、文字列を数値としてソートするには、IComparerを使用するために行くことができます。 これはすでに行われていると、あなたがディレクトリを取得した後、サンプル

here

編集中に見つけることができます:

string[] dirs = System.IO.Directory.GetDirectories(); 
var numComp = new NumericComparer(); 
Array.Sort(dirs, numComp); 

のdirsの最後の項目は、修正された「あなたの最後です"ディレクトリ。

1

この関数は、LINQを使用して最後の更新ディレクトリパスを取得します。

public string GetLatestUpdate(string path) 
{ 
    if (!path.EndsWith("\\")) path += "\\"; 
    return System.IO.Directory.GetDirectories(path) 
        .Select(f => new KeyValuePair<string, long>(f, long.Parse(f.Remove(0, (path + "Update").Length)))) 
        .OrderByDescending(kvp => kvp.Value) 
        .First().Key; 
} 
0

あなたは、フォルダの作成日に頼ることができれば、あなたはMoreLinq's MaxBy()を利用することによって、これを簡略化することができます。参考のために

string updatesFolder = "D:\\TEST\\Updates"; // Your path goes here. 
var newest = Directory.EnumerateDirectories(updatesFolder, "Update*") 
         .MaxBy(folder => new DirectoryInfo(folder).CreationTime); 

を、MaxBy()の実装は次のとおりです。

public static class EnumerableMaxMinExt 
{ 
    public static TSource MaxBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> selector) 
    { 
     return source.MaxBy(selector, Comparer<TKey>.Default); 
    } 

    public static TSource MaxBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> selector, IComparer<TKey> comparer) 
    { 
     using (IEnumerator<TSource> sourceIterator = source.GetEnumerator()) 
     { 
      if (!sourceIterator.MoveNext()) 
      { 
       throw new InvalidOperationException("Sequence was empty"); 
      } 

      TSource max = sourceIterator.Current; 
      TKey maxKey = selector(max); 

      while (sourceIterator.MoveNext()) 
      { 
       TSource candidate = sourceIterator.Current; 
       TKey candidateProjected = selector(candidate); 

       if (comparer.Compare(candidateProjected, maxKey) > 0) 
       { 
        max = candidate; 
        maxKey = candidateProjected; 
       } 
      } 

      return max; 
     } 
    } 
} 
+0

私は日付を使用することはできません: –

関連する問題