【问题标题】:Filtering out bad directory paths过滤掉错误的目录路径
【发布时间】:2011-09-08 05:01:02
【问题描述】:

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

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

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

谢谢!

【问题讨论】:

    标签: c# arrays filtering


    【解决方案1】:

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

    【讨论】:

    • 更改了我的代码以包含列表(没想到这一点)非常感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2012-06-15
    • 2015-01-07
    • 1970-01-01
    • 2013-04-12
    • 2017-02-14
    • 2020-10-31
    • 2022-11-02
    • 2018-11-09
    相关资源
    最近更新 更多