4

Right now I'm trying to get the start times of all the process that are running on a computer. The code I have for it so far is this:

foreach (Process item in Process.GetProcesses())
    txtActivity.AppendText(item.StartTime.ToString());

The problem is that I run into this error:

System.ComponentModel.Win32Exception: 'Access is denied'

So far everything I've seen on how to fix this error has been unhelpful. I've tried running it with Admin access and that didn't work, and all of the other proposed methods on threads like this one have either not worked or have been impossible to perform on my machine. Any fresh help to this problem is appreciated.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
TermSpar
  • 401
  • 3
  • 13

2 Answers2

5

Some processes seem to behave that way, but I don't know exactly why. You can find out which ones are doing it by wrapping your code in a try/catch, so you can look at the name of the processes that are throwing the exception.

For example

private static void Main()
{
    foreach (Process item in Process.GetProcesses())
    {
        try
        {
            Console.WriteLine($"{item.ProcessName} started at: {item.StartTime}");
        }
        catch(Exception e)
        {
            WriteColoredLine($"{e.Message}: {item.ProcessName}", ConsoleColor.Red);
        }
    }

    GetKeyFromUser("Done! Press any key to exit...");
}

private static void WriteColoredLine(string message, ConsoleColor color)
{
    Console.ForegroundColor = color;
    Console.WriteLine(message);
    Console.ResetColor();
}

Sample Output

enter image description here

Rufus L
  • 36,127
  • 5
  • 30
  • 43
0

In Windows some process properties are protected - they are called "Secure property" and Process.StartTime is one of the secure properties.

You can only read secure properties if the application User is a member part of the Performance Log users group or enable it in computer security policy.

To enable this

  1. Click Start, click in the Search box, type secpol.msc, and press ENTER. The Local Security Policy snap-in will open in Microsoft Management Console.

  2. In the navigation pane, expand Local Policies and click User Rights Assignment.

  3. In the console pane, right-click Log on as a batch job and click Properties.

  4. In the Properties page, click Add User or Group.

  5. In the Select Users or Groups dialog box, click Object Types. Select Groups in the Object Types dialog box and click OK.

  6. Type Performance Log Users in the Select Users or Groups dialog box and then click OK.

  7. In the properties page, click OK.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Walter Verhoeven
  • 3,867
  • 27
  • 36