如何在c#中获取当前活动进程的持续时间?

时间:2016-06-22 14:17:03

标签: c#

有没有办法知道当前窗口的窗口名称和持续时间在c#中打开,并在窗口关闭时获得回调?

2 个答案:

答案 0 :(得分:1)

using System.Diagnostics;

Process[] processlist = Process.GetProcesses();

foreach (Process process in processlist)
{
    if (!String.IsNullOrEmpty(process.MainWindowTitle))
        Console.WriteLine("Process: {0} ID: {1} Window title: {2}" duration: {3}" , process.ProcessName, process.Id, process.MainWindowTitle, process.duration);
}

// i'm not sure if process.duration actually exists but it would be something like that 

答案 1 :(得分:0)

是的,使用Process课程,您可以获得该信息。

using System.Diagnostics;

public void GetProcessesInfo()
{
    Process[] allProcesses = Process.GetProcesses();

    foreach (Process process in allProcesses)
    {
        try
        {
            string windowName = process.MainWindowTitle;
            TimeSpan duration = DateTime.Now - process.StartTime;
            process.EnableRaisingEvents = true;
            process.Exited += new EventHandler(process_Exited);
        }
        catch(System.ComponentModel.Win32Exception)
        {
            //access to that process was denied
        }
    }
}

void process_Exited(object sender, EventArgs e)
{
    //a process has exited
}