This is going to take a while to explain but bear with me.
I'm automating the creation of project and lead records in our database based on the existence of sub directories at a certain path. The directory structure is three levels deep. I point the code at the top level return the subdirectories (regions) into an array of DirectoryInfo objects like so:
DirectoryInfo[] classARegions = dir.GetDirectories("*", SearchOption.TopDirectoryOnly);
The directories I want to work with are subs of this region level so I iterate through the DirectoryInfo array with:
foreach (DirectoryInfo regio开发者_JS百科n in classARegions)
I then iterate through the region DirectoryInfo objects subdirectories in the same way:
DirectoryInfo[] prospects = region.GetDirectories("*", SearchOption.TopDirectoryOnly);
Everything is ok up to this point. The problem I run into is after I finish processing the first region's subdirectories and head back through the ForEach loop I find that my prospects array isn't re-initialized as empty. Instead it now includes all the sub directories from the prior iteration in addition to the subdirectories for the current iteration.
My question boils down whether I'm doing this wrong or if there a way to empty the prospects array so it's only populated with subdirectories from this iteration's region?
* Entire Method is posted below.
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();
}
The solution to this was arrived at by digging in another direction thanks to the comment from @BrokenGlass.
Solution: I was skipping folders if the name lacked a "-" char. In my summary email I wasn't accounting for the skipped folders. The total # of folders came back higher than the sum of countAdded and countExisting. Walking through the code again led me to this line:
if (seperator == -1) continue;
I was leaving the loop anytime I hit a filename without a "-" character.
精彩评论