C# 管理窗口事件

标签 c# windows

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

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

最佳答案

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

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();
    }
}

关于C# 管理窗口事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/563185/

相关文章:

c# - 一个 exe 中的多个 Windows 服务

windows - !heap -s 在 windbg 中不显示不断增长的堆

php - 是否可以在 Windows 中使用 PHP 访问和设置 "File is ready for archiving"标志?

c# - 为标志类型枚举定义 "catchall"是否明智?

c# - 关闭联锁保护程序

c# - 强密码正则表达式

windows - 如何从文件中获取特定单词的计数

c# - 如果用户没有安装 .NET,我如何部署 C# 应用程序?

c# - slider 最大值 = textbox1.text

python - 如何使用Python/PyQT/Win32获取Windows任务栏的高度