C# Collection 被修改错误,从文件中读取乱码文本

标签 c# .net sdk buildmaster

我是 C# 新手,被要求在我们的部署软件的插件中编写自定义任务,但我就是无法理解这一点。我只是想将部署服务器上某个目录中的一些数据记录到输出日志中,但我只记录了第一个文件(然后甚至文本也是乱码,我认为它以某种方式加载了错误的字节)在收到有关“附加信息:集合已修改;枚举操作可能无法执行。”的奇怪错误之前。

这是我到目前为止的代码:

class Clense : AgentBasedActionBase
{
    public string dataPath { get; set; }

    protected override void Execute()
    {
        IFileOperationsExecuter agent = Context.Agent.GetService<IFileOperationsExecuter>();
        GetDirectoryEntryCommand get = new GetDirectoryEntryCommand() { Path = dataPath };
        GetDirectoryEntryResult result = agent.GetDirectoryEntry(get);
        DirectoryEntryInfo info = result.Entry;
        // info has directory information
        List<FileEntryInfo> myFiles = info.Files.ToList();
        foreach (FileEntryInfo file in myFiles)
        {
            Byte[] bytes = agent.ReadFileBytes(file.Path);
            String s = Encoding.Unicode.GetString(bytes);
            LogInformation(s);
            // myFiles.Remove(file); 
        }
    }
}

有人知道我可以做些什么来尝试解决这个问题吗?

更新

删除 myFiles.Remove() 修复了错误(我以为它会循环太多次,但事实并非如此),看起来我现在每个文件都有一个日志条目,但消息仍然是乱码。有谁知道为什么会发生这种情况?

最佳答案

您可以以相反的方向迭代myFiles集合(这样在删除每个单独的文件时就不会损坏集合),或者您可以简单地完成迭代后清除集合(这将完成相同的事情)。

关于C# Collection 被修改错误,从文件中读取乱码文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26129828/

相关文章:

c# - 如何将 ControlTemplate 转换为 WPF 中的 XAML 字符串?

c# - Kinect 应用程序的源逆向工程

c# - 从 Web API 帮助页面中排除媒体类型示例

.net - 使用 SQLExpress 进行多用户商业应用程序的经验?

c# - 在 C# 中将 Windows 窗体属性绑定(bind)到 ApplicationSettings 的最佳方法?

iphone - popViewController 不工作

c# - 如何保护手游的时效存档

c# - 通过 TcpClient 的网络流写入大数据的最佳方式

ios - 如何在iphone sdk中播放原始pcm数据

java - 我可以使用 Stormpath Java SDK 在 strompath 中验证用户帐户吗?