Sauron
Sauron

Reputation: 16923

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.

Upvotes: 6

Views: 14896

Answers (5)

Dmytro Vasyanovych
Dmytro Vasyanovych

Reputation: 3

System.Diagnostics.Process.GetProcessById(xxx).StartTime;//fails for certain processes with access denied

Upvotes: 0

Reputation:

You can get process metadata by inspecting the Process object returned by Process.GetProcessesByName().

Upvotes: 0

raven
raven

Reputation: 18145

In Code

Suppose you want to find the start time of Notepad, currently running with PID 4548. You can find it, using the PID or the process name, and print it to the debug window like this:

//pick one of the following two declarations
var procStartTime = System.Diagnostics.Process.GetProcessById(4548).StartTime;
var procStartTime = System.Diagnostics.Process.GetProcessesByName("notepad").FirstOrDefault().StartTime;
System.Diagnostics.Debug.WriteLine(procStartTime.ToLongTimeString());

In Windows

You can use Process Explorer, which has an option to display the process start time, or you can list all the currently running processes, and their start times, from the command line with the following:

wmic process get caption,creationdate

Upvotes: 0

Rashmi Pandit
Rashmi Pandit

Reputation: 23858

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

Upvotes: 10

russau
russau

Reputation: 9108

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

Upvotes: 8

Related Questions