2011-09-07 73 views
1

目前,我有下面的代碼,就會計算的目錄路徑的數量不存在:過濾掉不好的目錄路徑

int failedImports = 0; 
    if (this.fileExplorer.ShowDialog() == DialogResult.OK) 
    { 
     string importFile = this.fileExplorer.FileName; 
     string importedDirs = File.ReadAllText(importFile); 
     var result = Regex.Split(importedDirs, "\r\n|\r|\n"); 
     foreach (string item in result) 
     { 
      if (!String.IsNullOrEmpty(item.ToString()) && Directory.Exists(item.ToString())) 
      { 
       this.lstbDirectories.Items.Add(item); 
      } 
      else 
      { 
       string desc; 
       failedImports++; 
       if (failedImports > 1) { desc = "Directories"; } else { desc = "Directory"; } 
       lblImportStatus.Text = (String.Format("{0} {1} failed to be\nimported. Please check that\nthey exist and try again.", failedImports, desc)); 
      } 
     } 
    } 

我將如何去寫每一個失敗目錄導入到一個數組等等我可以向用戶顯示失敗的條目?

謝謝!

回答

3

創建一個List<string>並針對每個壞字符串調用Add()

+0

更改我的代碼以包含列表(沒想到)非常感謝您的幫助! – MrDKOz