0

这需要一段时间来解释,但请耐心等待。

我正在根据某个路径上存在的子目录自动在我们的数据库中创建项目和潜在客户记录。目录结构深三层。我将代码指向顶层,将子目录(区域)返回到 DirectoryInfo 对象数组中,如下所示:

DirectoryInfo[] classARegions = dir.GetDirectories("*", SearchOption.TopDirectoryOnly);

我要使用的目录是此区域级别的子目录,因此我遍历 DirectoryInfo 数组:

foreach (DirectoryInfo region in classARegions)

然后我以相同的方式遍历区域 DirectoryInfo 对象子目录:

DirectoryInfo[] prospects = region.GetDirectories("*", SearchOption.TopDirectoryOnly);

到目前为止一切正常。我遇到的问题是在我处理完第一个区域的子目录并返回 ForEach 循环后,我发现我的潜在客户数组没有重新初始化为空。相反,除了当前迭代的子目录之外,它现在还包括先前迭代中的所有子目录。

我的问题归结为我做错了,还是有办法清空潜在客户数组,所以它只填充了这个迭代区域的子目录?

*整个方法贴在下面。

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

1 回答 1

0

感谢@BrokenGlass 的评论,通过从另一个方向挖掘来解决这个问题。

解决方案:如果名称缺少“-”字符,我会跳过文件夹。在我的摘要电子邮件中,我没有考虑跳过的文件夹。返回的文件夹总数高于 countAdded 和 countExisting 的总和。再次浏览代码将我带到这一行:

if (seperator == -1) continue;

每当我遇到没有“-”字符的文件名时,我都会离开循环。

于 2014-01-28T19:31:11.443 回答