对象变量仅显示最后一个值

时间:2018-05-31 20:14:30

标签: c# arrays ssis etl subdirectory

以下代码仅返回循环中的最新值。我需要做些什么才能显示正在迭代的所有值?我使用此方法而不是@Mapper public interface MyMapper { default PersonDTO personBLOToPersonDTO(PersonBLO personBLO) { if (personBLO == null) { return null; } PersonDTO dto = personToPersonDTO(personBlo.getPerson()); // the rest of the mapping return dto; } PersonDTO personToPersonDTO(PersonBLO source); } 的原因是因为有一个我无法访问的文件夹路径。我确实尝试将SearchOption.AllDirectoryUnauthorizedAccessExceptiontry一起使用,但是返回一个空值,因为它一直在终止循环。

catch

1 个答案:

答案 0 :(得分:0)

尝试删除函数外的FileList声明,同时删除循环外的Dts.Variables["User::objDirectoryList"].Value = FileList;

System.Collections.ArrayList FileList = new System.Collections.ArrayList();

public void Main()
{
    string path = @"drive"; // TODO  
    ApplyAllFiles(path, ProcessFile);
    Dts.Variables["User::objDirectoryList"].Value = FileList;
}    

public void ProcessFile(string path) 
{
    /* ... */
}

public void ApplyAllFiles(string folder, Action<string> fileAction)
{

    List<string> logger = new List<string>();

    DateTime DateFilter = DateTime.Now.AddMonths(-6);

    foreach (string file in Directory.GetDirectories(folder))
    {
        fileAction(file);
    }
    foreach (string subDir in Directory.GetDirectories(folder))
    {
        string rootfolder = "root folder";

        if (subDir.Contains(rootfolder))
        {   
            FileList.Add(subDir);
            //MessageBox.Show(subDir);
        }
        try
        {
            ApplyAllFiles(subDir, fileAction);
        }
        catch (UnauthorizedAccessException e)
        {
            logger.Add(e.Message);

        }
        catch (System.IO.DirectoryNotFoundException e)
        {
            logger.Add(e.Message);
        }
    }
}