Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting time of a process

Tags:

c#

.net

How do I retrieve the starting time of a process using c# code? I'd also like to know how to do it with the functionality built into Widows, if possible.

like image 502
Sauron Avatar asked May 29 '09 04:05

Sauron


2 Answers

 public DateTime GetProcessStartTime(string processName)
 {
        Process[] p = Process.GetProcessesByName(processName);
        if (p.Length <= 0) throw new Exception("Process not found!");
        return p[0].StartTime;
 }

If you know the ID of the process, you can use Process.GetProcessById(int processId). Additionaly if the process is on a different machine on the network, for both GetProcessesByName() and GetProcessById() you can specify the machine name as the second paramter.

To get the process name, make sure the app is running. Then go to task manager on the Applications tab, right click on your app and select Go to process. In the processes tab you'll see your process name highlighted. Use the name before .exe in the c# code. For e.g. a windows forms app will be listed as "myform.vshost.exe". In the code you should say

 Process.GetProcessesByName("myform.vshost"); 
like image 52
SO User Avatar answered Sep 22 '22 10:09

SO User


Process has a property "StartTime": http://msdn.microsoft.com/en-us/library/system.diagnostics.process.starttime.aspx

Do you want the start time of the "current" process? Process.GetCurrentProcess will give you that: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.getcurrentprocess.aspx

like image 35
russau Avatar answered Sep 21 '22 10:09

russau