C# 管理窗口事件

2024-04-25

我想使用 C# 从 Windows 事件日志中删除事件。

谁能指出我如何实现这一目标的正确方向?


Easy :).
但删除看起来像从数组中删除项目,您需要复制所有数组,但需要删除的项目除外。
有一个示例,如何“从日志中删除项目具有非偶数索引的每个项目”。

using System;
using System.Collections;
using System.Diagnostics;
using System.Threading;

class EventLogEntryCollection_Item
{
    /// <summary>
    /// Prints all.
    /// </summary>
    /// <param name="myEventLogEntryCollection">My event log entry collection.</param>
    private static void PrintAll(EventLogEntryCollection myEventLogEntryCollection)
    {
        for (int i = 0; i < myEventLogEntryCollection.Count; i++)
        {
            Console.WriteLine("The Message of the EventLog is :"
               + myEventLogEntryCollection[i].Message);
        }
    }

    /// <summary>
    /// Creates the new log message.
    /// </summary>
    /// <param name="LogName">Name of the log.</param>
    /// <param name="message">The message.</param>
    private static void CreateNewLogMessage(string LogName, string message)
    {
        EventLog myEventLog = new EventLog();
        myEventLog.Source = LogName;
        myEventLog.WriteEntry(message);
        myEventLog.Close();
    }

    /// <summary>
    /// Mains this instance.
    /// </summary>
    public static void Main()
    {
        try
        {

            // Check if the source exists.
            if (!EventLog.SourceExists("MySource"))
            {
                // Create the source.
                // An event log source should not be created and immediately used.
                // There is a latency time to enable the source, it should be created
                // prior to executing the application that uses the source.
                EventLog.CreateEventSource("MySource", "MySource");
                Console.WriteLine("Creating EventSource");
                Console.WriteLine("Exiting, execute the application a second time to use the source.");
                Thread.Sleep(2000);
                // The source is created.  sleep to allow it to be registered.
            }

            string myLogName = EventLog.LogNameFromSourceName("MySource", ".");

            // Create a new EventLog object.
            EventLog myEventLog1 = new EventLog();
            myEventLog1.Log = myLogName;

            //Create test messages.
            myEventLog1.Clear();
            for (int i = 0; i < 20; i++)
            {
                CreateNewLogMessage("MySource", "The entry number is " + i);
            }

            // Obtain the Log Entries of "MyNewLog".
            EventLogEntryCollection myEventLogEntryCollection =
               myEventLog1.Entries;
            //myEventLog1.Close();
            Console.WriteLine("The number of entries in 'MyNewLog' = "
               + myEventLogEntryCollection.Count);

            // Copy the EventLog entries to Array of type EventLogEntry.
            EventLogEntry[] myEventLogEntryArray =
               new EventLogEntry[myEventLogEntryCollection.Count];
            myEventLogEntryCollection.CopyTo(myEventLogEntryArray, 0);

            Console.WriteLine("before deleting");
            // Display the 'Message' property of EventLogEntry.
            PrintAll(myEventLogEntryCollection);

            myEventLog1.Clear();

            Console.WriteLine("process deleting");
            foreach (EventLogEntry myEventLogEntry in myEventLogEntryArray)
            {
                //Console.WriteLine("The LocalTime the Event is generated is "
                // + myEventLogEntry.TimeGenerated + " ; " + myEventLogEntry.Message);
                if (myEventLogEntry.Index % 2 == 0)
                {
                    CreateNewLogMessage("MySource", myEventLogEntry.Message);
                }
            }

            Console.WriteLine("after deleting");
            PrintAll(myEventLogEntryCollection);

            myEventLog1.Close();
        }
        catch (Exception e)
        {
            Console.WriteLine("Exception:{0}", e.Message);
        }
        Console.ReadKey();
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C# 管理窗口事件 的相关文章

随机推荐

  • 如何使用 boost::serialization 序列化 std::vector ?

    class workflow private friend class boost serialization access template
  • Knockout :找出哪个可观察触发了计算

    我有一个具有多个可观察值的对象 计算中有没有一种方法可以知道哪些可观察的变化 从而知道哪个可观察触发了计算 先感谢您 马修 如果没有详细说明您想要实现的目标 我将发布此内容 希望它能有所帮助 跟踪更改的一个简单方法是使用 subscribe
  • 冲出地图

    所以我遇到这个问题 如果数组中的值高于输入的值 它应该执行某些操作 然后停止循环并且不要触及数组中的剩余值 这是到目前为止的代码 const percentages let enteredValue parseInt event targe
  • 更改 ggplot2 中的 Y 轴分隔符

    我有这段代码可以给出如下所示的图表 d ggplot df aes x Year y NAO Index width 8 geom bar stat identity aes fill NAO Index gt 0 position ide
  • Google 地图 v3 API - 自动完成(地址)

    尝试让我的谷歌地图应用程序自动完成工作 这是当前的代码 HTML
  • ScrollLeft() 不适用于 Firefox

    我这里有一个 jsfiddle http jsfiddle net stevea DyfGp 2 http jsfiddle net stevea DyfGp 2 其中外框包含延伸到视口之外的内框 这会强制出现水平滚动条 一个 向左走 Sc
  • Git 推送失败(Github/RStudio)

    我过去曾在这台机器上成功使用过 Git 但突然间我无法再将我的提交推送到 Github 存储库 我对 Git 工具链所做的最后一次更改是除了 Windows 客户端的 Github 之外还安装了 Git 1 8 5 2 除非我已经启动了 G
  • 真正的C静态局部变量替换?

    只是试图在 ObjectPascal Delphi 中实现 C C 静态局部变量的类似功能 让我们在 C 中编写以下函数 bool update position int x int y static int dx pow 1 0 rand
  • VS Code 终端无法识别 PATH 变量

    我在 PATH 中添加了一个目录 但 VS Code 中的终端无法识别我尝试从该目录运行的命令 exe 终端使用 cmd 而不是 power shell 我缺少什么 重新启动我的计算机 它就工作了 显然 VS Code 无法识别这些更改
  • 为什么插入查询有时需要很长时间才能完成?

    这是一个非常简单的问题 将数据插入表中通常工作正常 除了少数情况下插入查询需要几秒钟的时间 我是not尝试批量插入数据 因此 我为插入过程设置了一个模拟 以找出为什么插入查询偶尔需要超过 2 秒才能运行 Joshua建议索引文件可能正在调整
  • iPhone 应用程序和 PayPal

    我想将 PayPal 支付功能集成到我的本机 iPhone 应用程序中 而不使用 Web 界面 这样用户就不必离开当前应用程序 怎么可能 我应该使用 SOAP XML 请求 响应机制吗 我通过以下链接来的http www slideshar
  • 如何在keycloak登录页面实现Recaptcha

    我想在 keycloak 登录页面 如注册页面 中实现 recaptcha 我用所需的工厂类扩展了 UsernamePasswordForm 类 我什至还开设了行动要求课程 但我仍然看不到在提供程序选项卡中添加登录 我也修改了现有的logi
  • 如何禁用 C++ 宏中的警告

    在 Visual C 中 您可以使用 pragma 暂时禁用警告 pragma warning suppress 4307 如何禁用宏内的警告 例如 当我导致如下所示的 积分常量溢出 警告时 define TIMES A MILLION x
  • android 旋转器的屏幕方向处理

    我有一个活动 其中有一个旋转器 因为对于纵向和横向模式我有不同的布局所以我正在改变布局onConfigurationChanged method Override public void onConfigurationChanged Con
  • 多个资源的 REST 接口使用

    我目前正在通过 http 添加 REST API 到在线服务 我遇到了一个非常简单的问题 我找不到令我满意的答案 我主要有 2 个资源 用户 和 报告 正如您所猜测的那样 报告与用户相关联 与一个且仅一个 我的数据库中的外键 不管怎样 我有
  • Elastic Search 5.x 嵌套多个查询 C#

    我将 C 与这些 nuget 包一起使用
  • 有没有办法匹配规范中 Mockito 模拟对象的按名称调用参数?

    我正在使用一些方法测试一个对象和另一个对象之间的交互呼唤名字论据 但是 我不知道如何为该按名称调用参数创建参数匹配器 假设这是模拟对象的签名 def fn arg1 gt String arg2 Int Any 然后我真正想做的是测试是否使
  • 调整某一特定一侧的边框

    我正在使用 ListBox 的 controlTemplate 来显示集合 我想显示带有边框的所有项目 就像在网格中一样 所有行的大小相同 当我给每个 listBoxItem 一个边框时 两个项目之间的线具有双倍大小 由第一个项目的底部边框
  • 更改变量值 scss

    我在我的 scss 文件中定义了不同的变量 我在一些 scss 文件中使用了这些变量 变量 scss light theme rgba 94 161 215 0 3 dark theme 5EA1D7 darker theme 57647A
  • C# 管理窗口事件

    我想使用 C 从 Windows 事件日志中删除事件 谁能指出我如何实现这一目标的正确方向 Easy 但删除看起来像从数组中删除项目 您需要复制所有数组 但需要删除的项目除外 有一个示例 如何 从日志中删除项目具有非偶数索引的每个项目 us