这需要一段时间才能解释,但请耐心等待。
我根据特定路径上子目录的存在自动创建数据库中的项目和潜在客户记录。目录结构有三层深。我指出顶层的代码将子目录(区域)返回到DirectoryInfo对象的数组中,如下所示:
DirectoryInfo[] classARegions = dir.GetDirectories("*", SearchOption.TopDirectoryOnly);
我想要使用的目录是这个区域级别的子目录,因此我使用以下命令遍历DirectoryInfo数组:
foreach (DirectoryInfo region in classARegions)
然后我以相同的方式遍历区域DirectoryInfo对象子目录:
DirectoryInfo[] prospects = region.GetDirectories("*", SearchOption.TopDirectoryOnly);
到目前为止一切都很好。我遇到的问题是在我完成处理第一个区域的子目录并返回ForEach循环之后,我发现我的前景数组没有重新初始化为空。相反,它现在包括除了当前迭代的子目录之外的先前迭代中的所有子目录。
我的问题归结为我是否做错了,或者是否有办法清空 prospect 数组,因此它只填充了此迭代区域的子目录?
* 整个方法发布在下面。
public string UpdateProspectInfo()
{
int countAdded = 0;
int countExisting = 0;
int countAddedOverall = 0;
int countExistingOverall = 0;
StringBuilder summary = new StringBuilder();
StringBuilder existing = new StringBuilder();
StringBuilder added = new StringBuilder();
string prospectDir = ConfigurationManager.AppSettings["ProspectDir"].ToString();
// get list of folders in Class A
DirectoryInfo dir = new DirectoryInfo(prospectDir);
DirectoryInfo[] classARegions = dir.GetDirectories("*", SearchOption.TopDirectoryOnly);
summary.Append("Found " + classARegions.Length + " Class A Regions.\r\n");
foreach (DirectoryInfo region in classARegions)
{
string regionName = (region.Name.Length > 50 ? region.Name.Substring(0, 50) : region.Name);
DirectoryInfo[] prospects = region.GetDirectories("*", SearchOption.TopDirectoryOnly);
summary.Append("\r\n Region: " + regionName + " contains " + prospects.Length + " prospect folders.\r\n");
foreach (DirectoryInfo prospect in prospects)
{
string projNum;
string projName;
int seperator = prospect.Name.IndexOf("-");
// go to next prospect if name doesn't contain a - character
if (seperator == -1)
continue;
projNum = prospect.Name.Substring(0, seperator);
projName = prospect.Name.Substring(seperator + 1);
ProjectCollection temp = new Select().From<Project>()
.Where("ProjectName").IsEqualTo(projName)
.ExecuteAsCollection<ProjectCollection>();
if (temp.Count < 1)
{
Project tempProj = new Project();
tempProj.ProjectNumber = projNum;
tempProj.ProjectName = projName;
tempProj.PMAssigned = "Joe Smith";
tempProj.PMUserID = 108;
tempProj.ProjectActivity = "Active";
tempProj.ProjectLead = "Lead";
//tempProj.Location = projNum.Substring(0,50);
tempProj.DirectoryPath = prospect.FullName;
tempProj.Save();
countAdded++;
added.Append(" " + projName + "\r\n");
}
else
{
((Project)temp[0]).DirectoryPath = prospect.FullName;
((Project)temp[0]).Save();
countExisting++;
}
}
// add summary for each region
summary.Append(" Added " + countAdded + " prospects.\r\n");
summary.Append(added.ToString());
summary.Append(" Processed " + countExisting + " prospects that already existed in the database.\r\n");
// update counts and continue to next region
countAddedOverall += countAdded;
countExistingOverall += countExisting;
countAdded = 0;
countExisting = 0;
}
return summary.ToString();
}
答案 0 :(得分:0)
由于@BrokenGlass的评论,通过挖掘另一个方向来解决这个问题。
解决方案:如果名称缺少“ - ”字符,我正在跳过文件夹。在我的摘要电子邮件中,我没有考虑跳过的文件夹。文件夹的总数高于countAdded和countExisting的总和。再次浏览代码使我走到了这一行:
if (seperator == -1) continue;
每当我点击没有“ - ”字符的文件名时,我就会离开循环。