-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMainProcess.cs
More file actions
155 lines (142 loc) · 6.19 KB
/
MainProcess.cs
File metadata and controls
155 lines (142 loc) · 6.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;
namespace FileMonitor
{
public static class MainProcess
{
public static Regex PathRegex = new Regex(@"^[A-Z]:\\(.+?\\)*.*$");
public static Config Config = new Config();
private static List<FileSystemWatcher> Watchers = new List<FileSystemWatcher>();
static MainProcess()
{
var configStr = FileHelper.Read(Config.ConfigPath);
try
{
if (!string.IsNullOrEmpty(configStr))
Config = JsonConvert.DeserializeObject<Config>(configStr);
}
catch { }
}
public static void SaveConfig(List<Form> forms)
{
foreach (var form in forms)
{
Config.WindowPos[form.Name] = new Pos(form.Top, form.Left, form.Height, form.Width);
}
FileHelper.Write(Config.ConfigPath, JsonConvert.SerializeObject(Config));
}
public static void InitWatchers()
{
Watchers.ForEach(t => t.Dispose());
Watchers = new List<FileSystemWatcher>();
//为设置的路径初始化监视器
if (Config?.FilePaths?.Count != 0)
{
foreach (var item in Config.FilePaths)
{
if (item.OriginPath == null || item.BackupPath == null)
continue;
if (item.Started && PathRegex.IsMatch(item.OriginPath) && PathRegex.IsMatch(item.BackupPath))
{
FileSystemWatcher watcher;
try
{
watcher = new FileSystemWatcher(item.OriginPath)
{
NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.CreationTime | NotifyFilters.Size,
Filter = "*"
};
watcher.Created += Watcher_Changed;
watcher.Changed += Watcher_Changed;
watcher.Renamed += Watcher_Changed;
Watchers.Add(watcher);
watcher.EnableRaisingEvents = true;
}
catch (Exception ex)
{
new string[] { Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "FileMonitor", "Log.json" }.Write_Append(JsonConvert.SerializeObject(ex));
item.Started = false;
}
}
else
{
item.Started = false;
}
}
}
}
public static void Dispose(int index)
{
Watchers.FirstOrDefault(t => t.Path == Config.FilePaths[index].OriginPath)?.Dispose();
Watchers.RemoveAll(t => t.Path == Config.FilePaths[index].OriginPath);
Config.FilePaths[index].Started = false;
}
public static void InitWatcher(int index)
{
var item = Config.FilePaths[index];
if (item.OriginPath == null || item.BackupPath == null)
return;
if (PathRegex.IsMatch(item.OriginPath) && PathRegex.IsMatch(item.BackupPath))
{
FileSystemWatcher watcher;
try
{
watcher = new FileSystemWatcher(item.OriginPath)
{
NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.CreationTime | NotifyFilters.Size,
Filter = "*"
};
watcher.Created += Watcher_Changed;
watcher.Changed += Watcher_Changed;
watcher.Renamed += Watcher_Changed;
Watchers.Add(watcher);
watcher.EnableRaisingEvents = true;
item.Started = true;
}
catch (Exception ex)
{
new string[] { Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "FileMonitor", "Log.json" }.Write_Append(JsonConvert.SerializeObject(ex));
item.Started = false;
}
}
else
{
item.Started = false;
}
}
private static void Watcher_Changed(object sender, FileSystemEventArgs e)
{
try
{
Thread.Sleep(0);
var originDic = e.FullPath.Replace($@"\{e.Name}", "");
string targetDicPath = Config.FilePaths.First(t => t.OriginPath == originDic).BackupPath;//目标备份文件夹
#region 备份目录下全部文件
//DirectoryInfo originRoot = new DirectoryInfo(originDic);
//FileInfo[] originFiles = originRoot.GetFiles();
//DirectoryInfo targetRoot = new DirectoryInfo(targetDicPath);
//var targetFileNames = targetRoot.GetFiles().Select(t => t.Name);
//var needBackup = originFiles;//.Where(t => !targetFileNames.Contains(t.Name));
//foreach (var file in needBackup)
//{
// var backupFilePath = targetDicPath.TrimEnd('\\') + $@"\[{DateTime.Now:yyyy年MM月dd日 hh时mm分ss秒}]{file.Name}";
// file.CopyTo(backupFilePath, true);
//}
#endregion
var file = new FileInfo(e.FullPath);
var backupFilePath = targetDicPath.TrimEnd('\\') + $@"\[{DateTime.Now:yyyy年MM月dd日 hh时mm分ss秒}]{file.Name}";
file.CopyTo(backupFilePath, true);
}
catch (Exception ex)
{
new string[] { Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "FileMonitor", "Log.json" }.Write_Append(JsonConvert.SerializeObject(ex));
}
}
}
}