Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Access is denied" when trying to get process start time

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.

like image 897
TermSpar Avatar asked Oct 13 '25 05:10

TermSpar


1 Answers

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

like image 183
Rufus L Avatar answered Oct 14 '25 19:10

Rufus L