Sauron Sauron - 1 month ago 21
C# Question

Starting time of a process

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.

Answer
 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");