gpt4 book ai didi

C# Collection was modified error with garbled text reading from 文件

转载 作者:太空宇宙 更新时间:2023-11-03 10:39:08 38 4
gpt4 key购买 nike

我是 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 was modified error with garbled text reading from 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26129828/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com