C# 筛选出错误的目录路径
我目前有以下代码,将计算不存在的目录路径数:C# 筛选出错误的目录路径,c#,arrays,filtering,C#,Arrays,Filtering,我目前有以下代码,将计算不存在的目录路径数: int failedImports = 0; if (this.fileExplorer.ShowDialog() == DialogResult.OK) { string importFile = this.fileExplorer.FileName; string importedDirs = File.ReadAllText(importFile); var result
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));
}
}
}
如何将每个失败的目录导入写入一个数组,以便向用户显示失败的条目
谢谢大家! 创建一个
列表
并对每个错误字符串调用Add()
。将我的代码更改为包含列表(没有想到这一点)非常感谢您的帮助!