过滤掉错误的目录路径

时间:2011-09-07 20:59:33

标签: c# arrays filtering

我目前有以下代码,它将计算不存在的目录路径数:

    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));
            }
        }
    }

我如何将每个失败的目录导入写入数组,以便向用户显示失败的条目?

谢谢!

1 个答案:

答案 0 :(得分:2)

创建List<string>并在每个错误的字符串上调用Add()