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





        }
    }
}
imsome1
  • 1,182
  • 4
  • 22
  • 37

1 Answers1

2

If you want to fix this in the application do the following.

  1. Add the manifest file to your application and set the execution level as shown below.

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

Post this the application will ask for Admin Access when executed.

Soumen Mukherjee
  • 2,953
  • 3
  • 22
  • 34