【发布时间】:2026-02-13 22:55:03
【问题描述】:
我是 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时不能执行myFiles.Remove(file);。只要搜索错误,你应该找到many duplicates on *。 -
我确实看过那些,但我不想读两次文件。
-
试试
Encoding.UTF8而不是Unicode。
标签: c# .net sdk buildmaster