2015-10-19 55 views
0

我要出去放像1.2.3.4.5 ..但在這段代碼中我喜歡的值到1,10,100,101升序排序豐富的文本框.. plz幫助我的朋友如何使用C#

private void btn_load_Click_1(object sender, EventArgs e) 
{ 
    { 
     if (textBox1.Text != "") 
     { 
      richTextBox1.Clear(); 
      string tt = @"" + textBox1.Text; 
      String sdira = @"" + textBox1.Text; 
      string[] arrays = Directory.GetFiles(sdira, "*", SearchOption.AllDirectories) 
       .Select(x => Path.GetFileName(x)) 
       .ToArray();// get only file name and extention 
      foreach (string name in arrays) 
      { 
       //StringBuilder sb = new StringBuilder(); 
       richTextBox1.Text += name + "\n"; 
       //i want get out put like 1.2.3.4.5.. 
       //but in this code i got values like 1,10,100,101.. 
       //plz help me friends[first img is my current out put][1] 
      } 
     } 
    } 

Sample

+0

textbox1.text包含什麼? () –

+0

您應該先對數據進行排序,然後將這些項目追加到'StringBuilder'中,最後將結果添加到'RichTextBox'中。 –

+0

textbox1.text是路徑 – dilhan

回答

0

如果你想在字符串作爲數字而不是字典順序必須排序,解析他們:

var orderedFiles = Directory.EnumerateFiles(sdira, "*", SearchOption.AllDirectories) 
    .Select(x => new { file = x, nameNumber = Path.GetFileName(x).TryGetInt32() }) 
    .Where(x => x.nameNumber.HasValue) 
    .OrderBy(x => x.nameNumber.Value) 
    .Select(x => x.file); // or x.nameNumber.Value is you want the number 

這裏的解析擴展方法是LINQ查詢中使用:

public static int? TryGetInt32(this string item, IFormatProvider formatProvider = null, NumberStyles nStyles = NumberStyles.Any) 
{ 
    if (formatProvider == null) formatProvider = NumberFormatInfo.CurrentInfo; 
    int i = 0; 
    bool success = int.TryParse(item, nStyles, formatProvider, out i); 
    if (success) 
     return i; 
    else 
     return null; 
}