【问题标题】:Object variable only showing the last value对象变量只显示最后一个值
【发布时间】:2018-05-31 20:14:29
【问题描述】:

下面的代码只返回循环中的最新值。我需要做什么才能显示所有正在迭代的值?我使用这种方法而不是SearchOption.AllDirectory 的原因是因为有一个我无法访问的文件夹路径。我确实尝试将UnauthorizedAccessExceptiontrycatch 一起使用,但这会返回一个空值,因为它会继续终止循环。

public void Main()
{
    string path = @"drive"; // TODO  
    ApplyAllFiles(path, ProcessFile);
}    

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

public void ApplyAllFiles(string folder, Action<string> fileAction)
{
    System.Collections.ArrayList FileList = new System.Collections.ArrayList();
    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);
            Dts.Variables["User::objDirectoryList"].Value = FileList;
           //MessageBox.Show(subDir);
        }
        try
        {
            ApplyAllFiles(subDir, fileAction);
        }
        catch (UnauthorizedAccessException e)
        {
            logger.Add(e.Message);

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

【问题讨论】:

  • 你在说什么对象?可能是FileList? “回归”对你意味着什么?你在“展示”什么“价值观”,在哪里?
  • 你在说Dts.Variables["User::objDirectoryList"].Value = FileList;吗?如果是这种情况,您可能希望将其移到 for 循环之外
  • @EdPlunkett 我正在尝试将 FileList 数组分配给对象变量 User::objDirectoryList,它应该返回一个文件夹路径列表。但是,现在它只返回列表中的最后一个文件夹路径。
  • @Nilesh 是的,我有,但它不会运行任何东西。

标签: c# arrays ssis etl subdirectory


【解决方案1】:

尝试去掉函数外的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);
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-19
    • 1970-01-01
    • 1970-01-01
    • 2022-08-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多