using System.Diagnostics;private Stopwatch stw = new Stopwatch();private void Form1_Load(object sender, EventArgs e)
{
stw.Start();
}private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult dr = MessageBox.Show("真的要退出?", "退出", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
stw.Stop();
MessageBox.Show("程序共运行时间:" + stw.Elapsed.Seconds.ToString() + "秒");
e.Cancel = false;
}
else
{
e.Cancel = true;
}
}

解决方案 »

  1.   

    如果要精确的时间用api的getlocaltime()
    如果要简单用log4net,
    如果要粗糙用DateTime.now()
    如果还有其他要求..................
      

  2.   

    DateTime dt = DateTime.Now;System.Threading.Thread.Sleep(10000);
       
    TimeSpan ts = DateTime.Now - dt;string mytime =  //ts.根据提示 选择你需要的时间表示....
      

  3.   

    System.Diagnostics.Stopwatch() wth=new Stopwatch() ;
    wth.Start();
     
    //干活wth.Stop();
    this.textBox1.Text=wth.Elapsed.ToString();   //得出运行时间
      

  4.   

    http://blog.csdn.net/gslcn/archive/2007/03/12/1527007.aspx