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

动画演示

相关推荐
小羊没烦恼!21 小时前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
神仙别闹21 小时前
基于C#+MySQL实现(WinForm)个人聊天室软件
c#
伞伞悦读1 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
C语言小火车1 天前
C/C++ 为什么需要编译器?
开发语言·c++
霍霍的袁1 天前
【C++】map 和 set 的使用 | 从用法到底层
开发语言·c++·学习·visual studio
kybs19911 天前
全球灾害数据分析可视化 毕业设计-附源码66794
vue.js·spring boot·mysql·安全·django·c#·asp.net
孙启超1 天前
【AI开发之Rust】第 11 课:智能指针与内部可变性
开发语言·后端·rust
此生决int1 天前
深入理解C++系列(20)——C++11(下)
开发语言·c++
CoderYanger1 天前
A.每日一题:835. 图像重叠
java·开发语言·程序人生·leetcode·面试·职场和发展·学习方法
伞伞悦读1 天前
【第37期】Python JSON 与配置详解:序列化、反序列化、嵌套结构和配置文件
开发语言·python·json