-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
449 lines (404 loc) · 17.8 KB
/
Form1.cs
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace File_Sync
{
public partial class Form1 : Form
{
DirectoryInfo directory1Info;
DirectoryInfo directory2Info;
FileSystemWatcher fileSystemWatcher1;
FileSystemWatcher fileSystemWatcher2;
public Form1()
{
InitializeComponent();
}
/*
// Remove from Alt+Tab dialog
protected override CreateParams CreateParams
{
get
{
var Params = base.CreateParams;
Params.ExStyle |= 0x80;
return Params;
}
}*/
private void Button_Path1_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
textBox_Path1.Text = folderBrowserDialog1.SelectedPath;
}
}
private void Button_Path2_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
textBox_Path2.Text = folderBrowserDialog1.SelectedPath;
}
}
private void Button_Apply_Click(object sender, EventArgs e)
{
try
{
directory1Info = new DirectoryInfo(textBox_Path1.Text);
directory2Info = new DirectoryInfo(textBox_Path2.Text);
#region Path Controls
if (!directory1Info.Exists)
{
MessageBox.Show("First directory doesn't exist.");
return;
}
if (!directory2Info.Exists)
{
MessageBox.Show("Second directory doesn't exist.");
return;
}
if (directory1Info.FullName == directory2Info.FullName)
{
MessageBox.Show("Should be different paths");
return;
}
#endregion
if (radioButton_FirstPath.Checked)
{
if (MessageBox.Show($"All data in {directory2Info.FullName} will be lost. Do you want to continue?", "WARNING", MessageBoxButtons.YesNo) == DialogResult.Yes)
SyncFolders(textBox_Path1.Text, textBox_Path2.Text);
else
return;
}
else
{
if (MessageBox.Show($"All data in {directory1Info.FullName} will be lost. Do you want to continue?", "WARNING", MessageBoxButtons.YesNo) == DialogResult.Yes)
SyncFolders(textBox_Path2.Text, textBox_Path1.Text);
else
return;
}
#region FileSystemWatcher Events
fileSystemWatcher1 = new FileSystemWatcher(textBox_Path1.Text);
fileSystemWatcher2 = new FileSystemWatcher(textBox_Path2.Text);
fileSystemWatcher1.EnableRaisingEvents = true;
fileSystemWatcher2.EnableRaisingEvents = true;
fileSystemWatcher1.IncludeSubdirectories = true;
fileSystemWatcher1.Changed += FileSystemWatcher1_CreateChange;
fileSystemWatcher1.Created += FileSystemWatcher1_CreateChange;
fileSystemWatcher1.Deleted += FileSystemWatcher1_Deleted;
fileSystemWatcher1.Renamed += FileSystemWatcher1_Renamed;
fileSystemWatcher2.IncludeSubdirectories = true;
fileSystemWatcher2.Changed += FileSystemWatcher2_CreateChange;
fileSystemWatcher2.Created += FileSystemWatcher2_CreateChange;
fileSystemWatcher2.Deleted += FileSystemWatcher2_Deleted;
fileSystemWatcher2.Renamed += FileSystemWatcher2_Renamed;
void FileSystemWatcher1_CreateChange(object innerSender, FileSystemEventArgs innerE)
{
fileSystemWatcher2.EnableRaisingEvents = false;
SyncSingle_CreateChange(directory1Info, directory2Info, innerE.FullPath);
fileSystemWatcher2.EnableRaisingEvents = true;
}
void FileSystemWatcher2_CreateChange(object innerSender, FileSystemEventArgs innerE)
{
fileSystemWatcher1.EnableRaisingEvents = false;
SyncSingle_CreateChange(directory2Info, directory1Info, innerE.FullPath);
fileSystemWatcher1.EnableRaisingEvents = true;
}
void FileSystemWatcher1_Deleted(object innerSender, FileSystemEventArgs innerE)
{
fileSystemWatcher2.EnableRaisingEvents = false;
SyncSingle_Delete(directory1Info, directory2Info, innerE.FullPath);
fileSystemWatcher2.EnableRaisingEvents = true;
}
void FileSystemWatcher2_Deleted(object innerSender, FileSystemEventArgs innerE)
{
fileSystemWatcher1.EnableRaisingEvents = false;
SyncSingle_Delete(directory2Info, directory1Info, innerE.FullPath);
fileSystemWatcher1.EnableRaisingEvents = true;
}
void FileSystemWatcher1_Renamed(object innerSender, RenamedEventArgs innerE)
{
fileSystemWatcher2.EnableRaisingEvents = false;
SyncSingle_Rename(directory1Info, directory2Info, innerE.OldFullPath, innerE.FullPath);
fileSystemWatcher2.EnableRaisingEvents = true;
}
void FileSystemWatcher2_Renamed(object innerSender, RenamedEventArgs innerE)
{
fileSystemWatcher1.EnableRaisingEvents = false;
SyncSingle_Rename(directory2Info, directory1Info, innerE.OldFullPath, innerE.FullPath);
fileSystemWatcher1.EnableRaisingEvents = true;
}
#endregion
button_Apply.Enabled = false;
button_Stop.Enabled = true;
}
catch (Exception exception)
{
Console.WriteLine("Error:");
Console.WriteLine(exception.Message);
#if DEBUG
throw exception;
#endif
}
}
private void SyncSingle_CreateChange(DirectoryInfo sourceDirectory, DirectoryInfo targetDirectory, string sourcePath)
{
try
{
FileInfo sourceFileInfo = new FileInfo(sourcePath);
DirectoryInfo sourceDirectoryInfo = new DirectoryInfo(sourcePath);
if (sourceFileInfo.Exists)
{
string targetFilePath = String.Copy(sourcePath);
targetFilePath = targetFilePath.Replace(sourceDirectory.FullName, targetDirectory.FullName);
// File.Copy() SOMETIMES throws 'used by antoher process' exception, so i believe i need to wait operating system to continue.
while (!IsFileReady(sourcePath)) Thread.Sleep(100);
File.Delete(targetFilePath);
File.Copy(sourcePath, targetFilePath, true);
}
else if (sourceDirectoryInfo.Exists)
{
string targetFolderPath = String.Copy(sourcePath);
targetFolderPath = targetFolderPath.Replace(sourceDirectory.FullName, targetDirectory.FullName);
Directory.CreateDirectory(targetFolderPath);
}
else
{
Console.WriteLine("Single sync unsuccessful, trying to sync the whole folder");
//if not successful force to copy whole folder and its files
SyncFolders(sourceDirectory.FullName, targetDirectory.FullName);
}
}
catch (Exception exception)
{
Console.WriteLine("Error:");
Console.WriteLine(exception.Message);
#if DEBUG
throw exception;
#endif
}
}
private void SyncSingle_Delete(DirectoryInfo sourceDirectory, DirectoryInfo targetDirectory, string sourcePath)
{
try
{
FileInfo sourceFileInfo = new FileInfo(sourcePath);
DirectoryInfo sourceDirectoryInfo = new DirectoryInfo(sourcePath);
try
{
try
{
string targetFilePath = String.Copy(sourcePath);
targetFilePath = targetFilePath.Replace(sourceDirectory.FullName, targetDirectory.FullName);
File.Delete(targetFilePath);
}
catch (Exception)
{
string targetFolderPath = String.Copy(sourcePath);
targetFolderPath = targetFolderPath.Replace(sourceDirectory.FullName, targetDirectory.FullName);
Directory.Delete(targetFolderPath);
}
}
catch (Exception)
{
Console.WriteLine("Single sync unsuccessful, trying to sync the whole folder");
//if not successful force to copy whole folder and its files
SyncFolders(sourceDirectory.FullName, targetDirectory.FullName);
}
}
catch (Exception exception)
{
Console.WriteLine("Error:");
Console.WriteLine(exception.Message);
#if DEBUG
throw exception;
#endif
}
}
private void SyncSingle_Rename(DirectoryInfo sourceDirectory, DirectoryInfo targetDirectory, string sourceOldPath, string sourceNewPath)
{
try
{
FileInfo sourceFileInfo = new FileInfo(sourceNewPath);
DirectoryInfo sourceDirectoryInfo = new DirectoryInfo(sourceNewPath);
if (sourceFileInfo.Exists)
{
string targetOldFilePath = String.Copy(sourceOldPath);
targetOldFilePath = targetOldFilePath.Replace(sourceDirectory.FullName, targetDirectory.FullName);
string targetNewFilePath = String.Copy(sourceNewPath);
targetNewFilePath = targetNewFilePath.Replace(sourceDirectory.FullName, targetDirectory.FullName);
//while (!IsFileReady(sourceNewPath)) Thread.Sleep(100);
File.Move(targetOldFilePath, targetNewFilePath);
}
else if (sourceDirectoryInfo.Exists)
{
string targetOldFolderPath = String.Copy(sourceOldPath);
targetOldFolderPath = targetOldFolderPath.Replace(sourceDirectory.FullName, targetDirectory.FullName);
string targetNewFolderPath = String.Copy(sourceNewPath);
targetNewFolderPath = targetNewFolderPath.Replace(sourceDirectory.FullName, targetDirectory.FullName);
Directory.Move(targetOldFolderPath, targetNewFolderPath);
}
else
{
Console.WriteLine("Single sync unsuccessful, trying to sync the whole folder");
//if not successful force to copy whole folder and its files
SyncFolders(sourceDirectory.FullName, targetDirectory.FullName);
}
}
catch (Exception exception)
{
Console.WriteLine("Error:");
Console.WriteLine(exception.Message);
#if DEBUG
throw exception;
#endif
}
}
/// <summary>
/// Sync folders
/// </summary>
/// <param name="sourcePath">Source Folder that won't be changed</param>
/// <param name="targetPath">Target Folder that will be the same as Source Folder</param>
/// <returns></returns>
private bool SyncFolders(string sourcePath, string targetPath)
{
try
{
DirectoryInfo sourceDirectoryInfo = new DirectoryInfo(sourcePath);
DirectoryInfo targetDirectoryInfo = new DirectoryInfo(targetPath);
CopyFiles(sourceDirectoryInfo, targetDirectoryInfo);
TrimFiles(sourceDirectoryInfo, targetDirectoryInfo);
Console.WriteLine($"Directory sync successful. Source: {sourcePath} , Target: {targetPath}");
return true;
}
catch (Exception e)
{
Console.WriteLine($"Directory sync unsuccessful. Source: {sourcePath} , Target: {targetPath}");
Console.WriteLine(e.Message);
#if DEBUG
throw e;
#endif
MessageBox.Show($"Sync unsuccessful.\r\n{e.Message}");
return false;
}
}
/// <summary>
/// Copy all files and sub-folders (including their files and folders recursively) to target folder. Returns true if operation successful.
/// </summary>
/// <param name="sourceDirectoryInfo">Source Directory</param>
/// <param name="targetDirectoryInfo">Target Directory</param>
private bool CopyFiles(DirectoryInfo sourceDirectoryInfo, DirectoryInfo targetDirectoryInfo)
{
try
{
foreach (FileInfo fileInfo in sourceDirectoryInfo.GetFiles())
{
fileInfo.CopyTo(Path.Combine(targetDirectoryInfo.ToString(), fileInfo.Name), true);
}
foreach (DirectoryInfo subDirectory in sourceDirectoryInfo.GetDirectories())
{
DirectoryInfo newDirectory = targetDirectoryInfo.CreateSubdirectory(subDirectory.Name);
CopyFiles(subDirectory, newDirectory);
}
Console.WriteLine($"Files copy successful. Source: {sourceDirectoryInfo.FullName} , Target: {targetDirectoryInfo.FullName}");
return true;
}
catch (Exception e)
{
Console.WriteLine($"Files copy unsuccessful. Source: {sourceDirectoryInfo.FullName} , Target: {targetDirectoryInfo.FullName}");
Console.WriteLine(e.Message);
#if DEBUG
throw e;
#endif
return false;
}
}
/// <summary>
/// Delete target-folder files that don't exist in source-folder. Returns true if operation successful.
/// </summary>
/// <param name="sourceDirectoryInfo">Source directory where the existence of files will be controlled</param>
/// <param name="targetDirectoryInfo">Target directory where the files will be deleted if they don't exist in source directory</param>
private bool TrimFiles(DirectoryInfo sourceDirectoryInfo, DirectoryInfo targetDirectoryInfo)
{
try
{
foreach (FileInfo fileInfo in targetDirectoryInfo.GetFiles())
{
FileInfo possibleSourceFileInfo = new FileInfo(Path.Combine(sourceDirectoryInfo.FullName, fileInfo.Name));
if (!File.Exists(possibleSourceFileInfo.FullName))
{
File.Delete(fileInfo.FullName);
}
}
foreach (DirectoryInfo targetSubDirectory in targetDirectoryInfo.GetDirectories())
{
DirectoryInfo possibleSourceDirectoryInfo = new DirectoryInfo(Path.Combine(sourceDirectoryInfo.FullName, targetSubDirectory.Name));
if (!possibleSourceDirectoryInfo.Exists)
{
targetSubDirectory.Delete(true);
}
else
{
TrimFiles(possibleSourceDirectoryInfo, targetSubDirectory);
}
}
Console.WriteLine($"Files trim successful. Target: {targetDirectoryInfo.FullName} , Source: {sourceDirectoryInfo.FullName}");
return true;
}
catch (Exception e)
{
Console.WriteLine($"Files trim unsuccessful. Target: {targetDirectoryInfo.FullName} , Source: {sourceDirectoryInfo.FullName}");
Console.WriteLine(e.Message);
#if DEBUG
throw e;
#endif
return false;
}
}
bool IsFileReady(string filePath)
{
try
{
using (Stream stream = new FileStream(filePath, FileMode.Open))
{
return true;
}
}
catch (Exception)
{
return false;
}
}
private void Button_Stop_Click(object sender, EventArgs e)
{
fileSystemWatcher1.Dispose();
fileSystemWatcher2.Dispose();
button_Stop.Enabled = false;
button_Apply.Enabled = true;
}
private void Form1_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(500);
this.Hide();
}
else if (this.WindowState == FormWindowState.Normal )
{
notifyIcon1.Visible = false;
}
}
private void NotifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
}
}