分享给有需要的人,代码质量勿喷。
DateTime xjDTStart = DateTime.Now;//程序开始的时间
#region 很多代码
#endregion
DateTime xjDTEnd = System.DateTime.Now;//中间代码运行之后的时间
TimeSpan xjTimeSpan = xjDTEnd.Subtract(xjDTStart);
string xjTime = xjTimeSpan.ToString("c").Substring(0, 8);//格式为 小时:分钟:秒 //还有很多其它显示方式
MessageBox.Show("所用时间=" + xjTime);