【发布时间】:2011-11-03 11:20:44
【问题描述】:
我已经这样做了...用于在晚上10点每24小时执行一次备份(使用mysql备份数据库)功能
它还会检查文件创建时间(backup.sql)(即),如果有任何文件在 24 小时内未创建,它将创建文件.....这是在备份数据库功能中完成的。 ..
但它并没有为每天晚上 10 点创建文件....
我每天晚上 10 点使用计时器创建文件...
这是我的代码....
public partial class BackupForm : Form
{
private static System.Timers.Timer _timer;
private Int32 _hours = 0;
private Int32 _runAt = 10;
public BackupForm()
{
}
private void BackupForm_Load(object sender, EventArgs e)
{
_hours = (24 - (DateTime.Now.Hour + 1)) + _runAt;
_timer = new Timer {Interval = _hours*60*60*1000};
_timer.Elapsed += new ElapsedEventHandler(Tick);
_timer.Start();
}
void Tick(object sender, ElapsedEventArgs e)
{
string hostname = MainHelper.getServer();
const string path = @"C:\folder\Access\backupdb\";
var listfiles = Directory.GetFiles(@"C:\folder\Access\backupdb\", "backup-*.zip").OrderByDescending(File.GetCreationTime).ToList();
var getfiles = new List<String>();
var files = listfiles.Select(Path.GetFileName).ToList();
var dt = DateTime.Now;
foreach(var file in files)
{
var creationtime = File.GetCreationTime(file);
var diff = DateTime.Now.Subtract(creationtime);
if(diff.Hours > 24 && diff.Days < 2 && creationtime.Month == dt.Month && creationtime.Year == dt.Year && hostname == "localhost" && _hours == 24)
{
backupDatabase();// here, i am doing backup database(creating backup.Zip file)
}
else if (_hours != 24)
{
_hours = 24;
_timer.Interval = _hours * 60 * 60 * 1000;
}
}
}
}
我不知道我哪里做错了......
我的目标是每天在给定时间创建一个文件(执行备份数据库功能),此外还必须检查文件创建时间(24 小时内)
有没有人帮忙解决这个问题..
任何示例代码都会对我在特定时间引发事件非常有帮助,包括检查 24 小时......
非常感谢...提前
【问题讨论】:
标签: c# .net windows winforms timer