/// <summary> /// 定时处理要备份的文件任务 /// </summary> private void BackupFileTask() { while (IsStart) { if (ListTool.HasElements(BackupFiles)) { //获取要备份的文件列表并复制样本 List <string> temp; lock (BackupFiles) { temp = BackupFiles; BackupFiles = new List <string>(); } using (var db = new Muse()) { foreach (var t in temp) { //要备份的文件存在 if (File.Exists(t)) { //文件属于要备份的文件目录 string filepath = DirTool.GetFilePath(t); BackupPaths path = Paths.FirstOrDefault(x => filepath.Contains(x.Path)); if (path != null) { //文件的MD5码以前没有备份过 string md5 = FileTool.GetMD5(t); bool isback = db.Any <BackupFiles>(x => x.FullPath == t && x.Md5 == md5, null); if (!isback) { string pathname = path.Path; //备份文件夹路径 string pathalias = path.Alias; //备份文件夹别名 string pathfile = t.Substring(pathname.Length, t.Length - pathname.Length); //截取备份文件子目录(相对备份文件夹) string fileext = "." + DateTimeConvert.CompactString(DateTime.Now) + Path.GetExtension(t); //设置后缀 string fullpath = DirTool.Combine(R.Settings.FileBackup.FileManBackup, pathalias, pathfile + fileext); //组合路径 //删除冗余 DeleteExcess(t); //备份文件 BackupFile(t, fullpath, md5); _FileCount++; } } } } } } Thread.Sleep(R.Settings.FileBackup.BACK_UP_INTERVAL); } }
private void BtAddPath_Click(object sender, EventArgs e) { FolderBrowserDialog dialog = new FolderBrowserDialog(); dialog.Description = "请选择要备份的文件夹"; if (dialog.ShowDialog() == DialogResult.OK) { string selPath = dialog.SelectedPath; //获取选中的目录 string path = DirTool.Combine(selPath, "\\"); //格式化选中的目录 string name = DirTool.GetPathName(selPath); //获取目录名称 List <BackupPaths> clashPath = R.Services.FBS.Paths.Where(x => x.Path.Contains(path) || path.Contains(x.Path)).ToList(); //查询冲突项 if (ListTool.HasElements(clashPath)) { string cp = ""; clashPath.ForEach(x => cp += (x.Path + ";")); //存在重合目录 MessageBox.Show(string.Format("您当前选择路径:{0},与之前选择的目录:{1},存在嵌套包含关系,请先从备份目录中移除,然后重新添加。", path, cp)); } else { UIEnableButton(false); Task.Factory.StartNew(() => { using (var db = new Muse()) { if (!db.Do <BackupPaths>().Any(x => x.Path == path)) { BackupPaths bp = new BackupPaths() { Path = path, Alias = Guid.NewGuid().ToString() }; if (db.Add(bp) > 0) { R.Services.FBS.Paths.Add(bp); //添加到列表 R.Services.FBS.AddToWatcherPath(bp.Path); //添加到监听 UIDgvPathAdd(name, null); //添加到列表UI } } } UIEnableButton(true); }); } } }
private void BtDelPath_Click(object sender, EventArgs e) { if (DgvPath.CurrentRow != null) { int row = DgvPath.CurrentRow.Index; string path = R.Services.FBS.Paths[row].Path; if (row >= 0) { using (var db = new Muse()) { BackupPaths bp = db.Get <BackupPaths>(x => x.Path == path, null); if (bp != null) { db.Del(bp, true); } R.Services.FBS.Paths.RemoveAt(row); } UIDgvPathDel(row); } } }