C# 进程的开始时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/924392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Starting time of a process
提问by Sauron
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.
如何使用 c# 代码检索进程的开始时间?如果可能的话,我还想知道如何使用 Widows 中内置的功能来实现。
采纳答案by SO User
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.
如果您知道进程的 ID,则可以使用 Process.GetProcessById(int processId)。另外,如果进程在网络上的不同机器上,对于 GetProcessesByName() 和 GetProcessById(),您可以将机器名称指定为第二个参数。
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
要获取进程名称,请确保应用程序正在运行。然后转到应用程序选项卡上的任务管理器,右键单击您的应用程序并选择转到进程。在进程选项卡中,您将看到突出显示的进程名称。在 c# 代码中使用 .exe 之前的名称。例如,Windows 窗体应用程序将被列为“myform.vshost.exe”。在代码中你应该说
Process.GetProcessesByName("myform.vshost");
回答by russau
Process has a property "StartTime": http://msdn.microsoft.com/en-us/library/system.diagnostics.process.starttime.aspx
进程有一个属性“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
你想要“当前”进程的开始时间吗?Process.GetCurrentProcess 会给你: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.getcurrentprocess.aspx
回答by raven
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:
假设您要查找当前使用 PID 4548 运行的记事本的开始时间。您可以使用 PID 或进程名称找到它,并将其打印到调试窗口,如下所示:
//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
在 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:
您可以使用Process Explorer,它有一个显示进程启动时间的选项,或者您可以从命令行使用以下命令列出所有当前正在运行的进程及其启动时间:
wmic process get caption,creationdate
回答by raven
回答by Dmytro Vasyanovych
System.Diagnostics.Process.GetProcessById(xxx).StartTime;//fails for certain processes with access denied