C# 项目06-计算程序运行时间

实现需求

记录程序运行时间,当程序退出后,保存程序运行时间,等下次程序再次启动时,继续记录运行时间

运行环境

Visual Studio 2022

知识点

TimeSpan

表示时间间隔。两个日期之间的差异的 TimeSpan 对象

C# 复制代码
TimeSpan P_TimeSpan;
DateTime G_DateTime = DateTime.Now;
P_TimeSpan = DateTime.Now - G_DateTime;
/*
 P_TimeSpan.Days;时间间隔的天数
 P_TimeSpan.Hours;时间间隔的小时
 P_TimeSpan.Minutes;时间间隔的分钟
 P_TimeSpan.Seconds;时间间隔的分钟
*/

断电保持

本项目用的方法是将数据保存到项目属性中,操作方法如下:

  1. 右键点击项目,然后点击属性

2、在设置中创建对应的变量和数据类型,范围默认为用户

3、利用窗体的FormClosing事件,保存数据

C# 复制代码
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            DateTime G_DateTime_Close = DateTime.Now;
            Properties.Settings.Default.RuntimeDays = P_TimeSpan.Days + Properties.Settings.Default.RuntimeDays;
            Properties.Settings.Default.RuntimeHours = P_TimeSpan.Hours + Properties.Settings.Default.RuntimeHours;
            Properties.Settings.Default.RuntimeMinutes = P_TimeSpan.Minutes + Properties.Settings.Default.RuntimeMinutes;
            Properties.Settings.Default.RuntimeSeconds = P_TimeSpan.Seconds + Properties.Settings.Default.RuntimeSeconds;
            Settings.Default.Save();
        }

4、读取数据

C# 复制代码
Properties.Settings.Default.RuntimeDays
Properties.Settings.Default.RuntimeHours
Properties.Settings.Default.RuntimeMinutes
Properties.Settings.Default.RuntimeSeconds

动画演示

相关推荐
二十雨辰7 分钟前
vite如何处理项目中的资源
开发语言·javascript
聆风吟º33 分钟前
远程录制新体验:Bililive-go与cpolar的无缝协作
开发语言·后端·golang
豆浆whisky2 小时前
netpoll性能调优:Go网络编程的隐藏利器|Go语言进阶(8)
开发语言·网络·后端·golang·go
蓝天白云下遛狗2 小时前
go环境的安装
开发语言·后端·golang
CAir22 小时前
go协程的前世今生
开发语言·golang·协程
@大迁世界2 小时前
Go 会成为“老生态”的新引擎吗?
开发语言·后端·golang
Absinthe_苦艾酒2 小时前
golang基础语法(六)Map
开发语言·后端·golang
-睡到自然醒~2 小时前
Golang 中的字符串:常见错误和最佳实践
开发语言·后端·golang
予非池物2 小时前
ubuntu安装go
开发语言·后端·golang
小先生8123 小时前
.NET Core项目中 Serilog日志文件配置
c#·.netcore