如何获取Winform程序的运行时间?

How to get the Winform program run time?

我在超市用quartz.net关机,22时:00.Trigger关机事件:

        public void Execute(IJobExecutionContext context)
        {               
            logger.Info("shutdown.....");
            try
            {
                StatusHelper.ShutdownComputerImpl(MachineOperType.Shutdown);
            }
            catch (Exception e)
            {
                logger.Error("Shutdown computer encount an error", e);
            }
        }

但是 quartz.net 会在第一次时触发作业 运行!!!!

所以我可以得到系统已经运行多少时间,如果不到10分钟,功能就无法execute.Like这个:

public void Execute(IJobExecutionContext context)
            {               
                logger.Info("shutdown.....");
                try
                {
                     if(GetCurrentProcessRuntime()>50)
                     {
                         StatusHelper.ShutdownComputerImpl(MachineOperType.Shutdown);
                     }
                }
                catch (Exception e)
                {
                    logger.Error("Shutdown computer encount an error", e);
                }
            }

我正在用C# Winform编程,visual studio 2013,我想获取程序运行的启动时间,如何获取?可能是程序运行 2小时,时间将超过2小时(或分钟)。

我没有测量代码 运行 多少时间,我想获得系统 运行 启动时间!!!

您可以尝试使用秒表class:

using System.Diagnostics.Stopwatch
class Program
{
    static void Main()
    {
    // Create new stopwatch
    Stopwatch stopwatch = new Stopwatch();

    // Begin timing
    stopwatch.Start();

    // Do something     

    // Stop timing
    stopwatch.Stop();

    // Write result
    Console.WriteLine("Time elapsed: {0}",
        stopwatch.Elapsed);
    }
}