C# 事件日志。删除选定的EventLogEntry
我想删除每次迭代中的条目。我可以做吗?这是我的代码
static void Main(string[] args)
{
el = new EventLog();
el.Log = "XMLWatcherLog";
el.Source = "XMLWatcherSource";
string netStr =string.Empty;
foreach (EventLogEntry entry in el.Entries)
{
netStr += "<item>" + "<path>" + entry.Message + "</path>";
// here i want to delete entry
}
}
I want to delete entry in every iteration. Can i do it? Here is my code
static void Main(string[] args)
{
el = new EventLog();
el.Log = "XMLWatcherLog";
el.Source = "XMLWatcherSource";
string netStr =string.Empty;
foreach (EventLogEntry entry in el.Entries)
{
netStr += "<item>" + "<path>" + entry.Message + "</path>";
// here i want to delete entry
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你不应该使用 foreach 因为你要修改列表(删除条目)。对于第一次迭代,代码可以正常工作,但在第二次迭代时,它将抛出异常,指出“集合已修改”。您可以使用简单的 for 循环来代替 foreach。
I think you should not use foreach because you are going to modify the list (delete entry). For first iteration code will work fine but on second iteration it will throw exception saying that 'collection was modified'. Instead of foreach you can use simple for loop.