有时候在办公室里写一些小软件会想着自动更新、每日更新、定时更新的功能,比如每天到点就发个邮件,到点了就统计一下当前的网络数据什么的。写代码的时候主要考虑:1、设置时间点;2、怎么定时运行。
之前在写网络拓扑信息统计的时候,写了个设置定时的窗口(SetTimeForm),外观长这样:
SetTimeForm的相关tips如下:
- 左边的ListBox显示已设置的时间点(HH:mm);
- 右边的textBox用以输入需新增的时间点,点【添加时间】按钮或按回车键,可以将textBox中的时间点添加至ListBox中;
- 在ListBox中选中需要删除的时间点(可多选),点击【删除时间】按钮,可以从ListBox中删除;
- 点击右上角关闭窗口时,会将ListBox中的时间点,保存到本地路径的一个txt文件里。
SetTimeForm的具体代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace zipTestForm
{
public partial class SetTimeForm : Form
{
private static String strTimeSpanFilePath = Application.StartupPath + @"\timeSpan.txt";
public SetTimeForm()
{
InitializeComponent();
//读取timeSpan.txt,load出之前保存的时间点数据
//并显示在listBox上
if (File.Exists(strTimeSpanFilePath))
{
StreamReader sr = new StreamReader(strTimeSpanFilePath, Encoding.Default);
String line;
while ((line = sr.ReadLine()) != null)
{
listBoxTimeTable.Items.Add(line);