老师说效率不高,那就这样吧,做个管理时间的小程序,看看一天能工作多长时间,给自己做一个统计。
实现功能:1,开始记录工作时间
2,暂停工作
3,显示一次工作持续时间和当天总工作时间
4,保存所有记录,查看记录
5,定时提示,如“已经连续工作两个小时,注意休息!”
界面:
主要代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.IO;
using System.Text;
using System.Timers;
using System.Windows.Forms;
using Microsoft.Win32;
///**************************
///XP,vs2008,windows窗体应用程序
///
///**************************
namespace Work_time_statistic_system
{
public partial class Form1 : Form
{
public DateTime dt_defaul = Convert.ToDateTime("00:00:00");
public DateTime dt_all=Convert.ToDateTime("00:00:00");
public DateTime dtFor_timeContinue = new DateTime();
public int count = 0;
//获得文件的当前路径
static string dir = Directory.GetCurrentDirectory();
//获取可执行文件的全部路径
string exeDir = dir + "/time_log.txt";
//获取一个秒时间间隔
static DateTime dtone = Convert.ToDateTime("2012-11-1 00:00:01");
static DateTime dtwo = Convert.ToDateTime("2012-11-1 00:00:00");
public TimeSpan span = dtone.Subtract(dtwo); //算法是dtone 减去 dtwo
//开始工作时间
public string starteTime = "";
//结束工作时间
public string endTime ="";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//记录读写预定义
String[] lines = File.ReadAllLines(exeDir, Encoding.Default);
StreamWriter sw = new StreamWriter(exeDir,true ,Encoding.Default );
//获取当前日期
System.DateTime dt = System.DateTime.Now.Date;
string strDt=dt.Date .ToString().Substring(0,9);
this.myDate.Text = strDt;
//检索记录中是否有当天信息
Boolean isHave=true ;
for (long i = 0; i < lines.Length; i++)
{
if (lines[i].ToString().Trim() == strDt)
{
isHave =false ;
}
}
this.time_all.Text = lines[lines.Length - 1].Remove(0, 35).ToString().Trim();
if (isHave)
{
sw.WriteLine(strDt);
sw.WriteLine("=========================================================");
sw.WriteLine(" startTime-endTime continueTime wholeTime");
this.time_all.Text = "0:00:00";
}
sw.Close();
}
private void workStart_Click(object sender, EventArgs e)
{
//按钮状态切换
this.workStart.Enabled = false;
this.workEnd.Enabled = true;
this.button1.Enabled = false;
//保存开始工作的系统时间
this.starteTime = System.DateTime.Now.ToLongTimeString().ToString();
//清零
dt_defaul = Convert.ToDateTime("0:00:00");
//设定累加初始值
dt_all = Convert.ToDateTime(this.time_all.Text);
//启动计时器
this.timer1.Start();
}
private void workEnd_Click(object sender, EventArgs e)
{
//按钮状态切换
this.workStart.Enabled = true ;
this.workEnd.Enabled = false ;
this.button1.Enabled = true;
//停止计时器
this.timer1.Stop();
//保存开始工作的系统时间
this.endTime = System.DateTime.Now.ToLongTimeString().ToString();
//显示前次持续工作时间
this.time_continueBf.Text = this.time_continueNow.Text;
//存储阶段时间数据
upDateTxt();
//本次持续工作时间清零
this.time_continueNow.Text = "0:00:00";
}
private void timer1_Tick(object sender, EventArgs e)
{
//累计计时
dt_defaul = dt_defaul + span;
dt_all =dt_all +span;
//显示当前持续工作时间
this.time_continueNow.Text = dt_defaul.ToLongTimeString().ToString();
//显示总工作时间
this.time_all.Text = dt_all.ToLongTimeString().ToString();
//两小时候弹出休息提示(暂时保留,以后再实现。。。)
count++;
if (count == 7200)
MessageBox.Show("注意!您已经连续工作两个小时了,MMS工作室提醒您注意身体健康!");
}
private void upDateTxt()
{
//获得文件的当前路径
string dir = Directory.GetCurrentDirectory();
//获取可执行文件的全部路径
string exeDir = dir + "/time_log.txt";
//记录读写预定义
String[] lines = File.ReadAllLines(exeDir, Encoding.Default);
StreamWriter sw = new StreamWriter(exeDir);
for (long i = 0; i < lines.Length; i++)
{
sw.WriteLine(lines[i].ToString());
}
sw.WriteLine(" " + this.starteTime + "-" + this.endTime + " " + this.time_continueNow.Text + " " + this.time_all.Text);
sw.Close();
}
private void button1_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start(exeDir);
}
}
}