0

When I run the code below I get Exception Unhandled System.Security.SecurityException:'Requested registry access is not allowed'. I have all the necessary machine access.

namespace EventViewer
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.WriteLine("Welcom to Event Log Viewer");
            Console.WriteLine("Please enter Retrieve to Retrieve EventLogs");
            string Retrieve = Console.ReadLine();
            string Logs = Retrieve;

            switch (Logs)
            {
                case "Retrieve":
                    EventLog eventLog;
                    eventLog = new EventLog();
                    eventLog.Log = "Security";
                    eventLog.Source = "Security-Auditiing";
                    eventLog.MachineName = "Cheetah";

                    var count = 0;
                    foreach (EventLogEntry log in eventLog.Entries)
                    {
                        if (count > 200)
                        {
                            return;
                        }
                        Console.Write("eventLog.Log: ", eventLog.Log);

                    }
                    break;
            }





        }
    }
}
4

1 回答 1

2

如果您想在应用程序中解决此问题,请执行以下操作。

  1. 将清单文件添加到您的应用程序并设置执行级别,如下所示。

     <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
    

发布此应用程序将在执行时要求管理员访问权限。

于 2017-10-04T08:57:03.330 回答