C# 事件日志。删除选定的EventLogEntry

发布于 2024-09-25 04:45:46 字数 407 浏览 1 评论 0原文

我想删除每次迭代中的条目。我可以做吗?这是我的代码

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

牵你的手,一向走下去 2024-10-02 04:45:46

我认为你不应该使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文