Is it possible, in C#, to get a list of running processes (not service processes, but actual applications) and get a DateTime of when the application started? Or a TimeSpan or even an integer of how long a process has been running?
Type the ps aux command to see all running process in Unix. Alternatively, you can issue the top command to view running process in Unix.
If you want to figure out how long a process has been running in Linux for some reason. We can easily check with the help of “ps” command. It shows, the given process uptime in the form of [[DD-]hh:]mm:ss, in seconds, and exact start date and time. There are multiple options are available in ps command to check this.
First, open the terminal window and then type: uptime command – Tell how long the Linux system has been running. w command – Show who is logged on and what they are doing including the uptime of a Linux box. top command – Display Linux server processes and display system Uptime in Linux too.
Process.GetProcesses
will retrieve a list of running processes.
Each Process
has a StartTime
property that
Gets the time that the associated process was started.
Simply subtract that from DateTime.Now
to get how long the process has been running.
static void Main(string[] args)
{
var procs = Process.GetProcesses();
foreach (var proc in procs) {
TimeSpan runtime;
try {
runtime = DateTime.Now - proc.StartTime;
}
catch (Win32Exception ex) {
// Ignore processes that give "access denied" error.
if (ex.NativeErrorCode == 5)
continue;
throw;
}
Console.WriteLine("{0} {1}", proc, runtime);
}
Console.ReadLine();
}
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