You can’t modify a collection you’re iterating over. In this case, a much better solution would be to create a list of entries to remove by iterating over the dictionary, and then iterate over that list, removing the entries from the dictionary:
List<string> removals = new List<string>();
DateTime now = DateTime.Now;
foreach(BruteforceEntry be in Entries.Values)
{
if (be.AddedTimeRemove <= now ||
(be.Unbantime <= now && be.Unbantime.Day == DateTime.Now.Day))
{
removals.Add(be.IPAddress);
}
}
foreach (string address in removals)
{
Entries.Remove(address);
}
Note that if you’re using .NET 3.5, you could use a LINQ query to express the first part:
List<string> removals = (from be in Entries.Values
where be.AddedTimeRemove <= now ||
(be.Unbantime <= now &&
be.Unbantime.Day == DateTime.Now.Day)
select be.IPAddress).ToList();