I'm new to C# and have been asked to write a custom task in a plugin for our deployment software, but I just can't wrap my head around this. I'm simply trying to log some data that is in a certain directory on my deployment server to the output log, but I only get the first file logged (and then even the text is garbled, I think it's loading the bytes wrong somehow) before getting strange errors about "Additional information: Collection was modified; enumeration operation may not execute."
Here is the code I have so far:
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);
}
}
}
Does anyone know what I can do to try to fix this?
Update
Removing the myFiles.Remove() fixed the error (I thought it would loop too many times but it doesn't) and it looks like I'm getting one log entry per file now, but the messages are still garbled. Does anyone have any idea why this is happening?