Take the 2-minute tour ×

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

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

            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)

                projNum = prospect.Name.Substring(0, seperator);
                projName = prospect.Name.Substring(seperator + 1);

                ProjectCollection temp = new Select().From<Project>()

                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;


                    added.Append("      " + projName + "\r\n");
                    ((Project)temp[0]).DirectoryPath = prospect.FullName;

            // add summary for each region
            summary.Append("    Added " + countAdded + " prospects.\r\n");
            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();
share|improve this question
What is the scope of prospects? It looks evident, but a complete code sample would benefit this question. –  Grant Thomas Mar 14 '11 at 12:30
Instead of posting three lines with some explanation you should post the whole iteration process to see where the problem comes from. –  Oliver Mar 14 '11 at 12:40
if you want all the subdirectories, etc. Why are you using SearchOption.TopDirectoryOnly not SearchOption.AllDirectories? –  Manatherin Mar 14 '11 at 12:44
I find this highly unlikely. The array is returned by the GetDirectories function. This would mean this function returns the wrong result. Besides posting more complete code, some more information on the directory structure could be relevant, or the entire structure if possible. –  Steven Jeuris Mar 14 '11 at 12:56
When you leave the scope for which the array reference is defined, then you no longer have a reference to that array. If you later reenter the same scope and redeclare the reference variable, then the array reference is created anew pointing to whichever array that you initialized it with this time around. Is there som piece of the puzzle that you have not described? Do you have more context (code) to show us? –  Simen S Mar 14 '11 at 12:59

1 Answer 1

up vote 0 down vote accepted

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.

share|improve this answer

Your Answer


