-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackupTools.cs
67 lines (60 loc) · 2.09 KB
/
BackupTools.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Backuper4000
{
enum MessageType
{
Success,
Information,
Error,
CriticalError
}
class BackupTools
{
public static void StartBackup(string sourcePath, string destinationPath, Action<string> log)
{
bool isSuccessfull = true;
try
{
CopyDirectory(sourcePath, destinationPath, log);
}
catch (Exception error)
{
isSuccessfull = false;
log("ERROR");
log(error.Message);
log("\n\nSTACKTRACE");
log(error.StackTrace);
}
if (isSuccessfull)
log("BACKUP FINISHED SUCCESSFULLY");
}
public static void CopyDirectory(string sourcePath, string destinationPath, Action<string> log)
{
if (!sourcePath.EndsWith(@"\"))
sourcePath += @"\";
Stack<string> directoryPaths = new Stack<string>();
directoryPaths.Push("");
while (directoryPaths.Count != 0)
{
string currentRelativePath = directoryPaths.Pop();
string currentSourcePath = Path.Combine(sourcePath, currentRelativePath);
string currentDestinationPath = Path.Combine(destinationPath, currentRelativePath);
Directory.CreateDirectory(currentDestinationPath);
foreach (string directoryPath in Directory.EnumerateDirectories(currentSourcePath))
{
directoryPaths.Push(directoryPath.Remove(0, sourcePath.Length));
}
foreach (string filePath in Directory.EnumerateFiles(currentSourcePath))
{
log(String.Format("<{0}>", Path.GetFileName(filePath)));
File.Copy(filePath, Path.Combine(currentDestinationPath, Path.GetFileName(filePath)));
}
}
}
}
}