I got this error:
(I can give more info from inspector)
System.ComponentModel.Win32Exception: 'access denied'
The exception was originally generated in this call stack:
System.Diagnostics.ProcessManager.OpenProcess(int, int, bool)
System.Diagnostics.Process.GetProcessHandle(int, bool)
System.Diagnostics.Process.GetProcessTimes()
System.Diagnostics.Process.StartTime.get()
ConsoleApp1.Program.Main(string[]) on Program.cs
There is the code:
(I was running this program as administrator since the beginning)
The correct answer is how to ignore exceptions, because the error occurs because certain processes cannot be read even if you have administrator privileges
using System.Diagnostics;
namespace ConsoleApp1
{
class Program
{
public class Win32Exception : System.Runtime.InteropServices.ExternalException
{
static void Main(string[] args)
{
var moment = DateTime.Now;
String rh = moment.Hour.ToString();
String rm = moment.Minute.ToString();
String rs = moment.Second.ToString();
Console.Title = moment.ToString("HH:mm:ss");
Process[] localAll = Process.GetProcesses();
/// Process[] procesos;
///procesos = Process.GetProcesses();
///
foreach (Process p in localAll)
{
/// Console.WriteLine(p.ProcessName);
try
{
var tmp = p.StartTime;
String h = p.StartTime.ToString("HH");
String m = p.StartTime.ToString("mm");
String s = p.StartTime.ToString("ss");
int x = Int32.Parse(rh);
int y = Int32.Parse(h);
if (x <= y)
{
Console.WriteLine($"{p.ProcessName} TIME= {p.StartTime.ToString("HH:mm:ss")}");
}
}
catch (Win32Exception)
{
continue;
}
}
Console.ReadKey();
}
}
}
}