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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With