tags:

views:

260

answers:

4

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.

+5  A: 

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

russau
How to know starttime without code. I mean from windows itself.
Sauron
That's a separate question, and probably not programming-related.
Matthew Flaschen
+1  A: 
 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");
Rashmi Pandit
How to know starttime without code. I mean from windows itself.
Sauron
What do you mean by 'from windows itself'? Your question states 'in C# code'.
Rashmi Pandit
I need with the code also. Just I want to know how to take it from windows?
Sauron
Edited my response. Does that answer your Q, or is there something else you need.
Rashmi Pandit
A: 

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
raven
A: 

Process.GetProcessesByName("processName");