-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBackupABExample.cs
More file actions
70 lines (53 loc) · 1.72 KB
/
BackupABExample.cs
File metadata and controls
70 lines (53 loc) · 1.72 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
// Created by Alex Meesters
// www.low-scope.com, www.alexmeesters.nl
// Licenced under the MIT Licence. https://en.wikipedia.org/wiki/MIT_License
using System;
using System.IO;
using System.Text;
public static class BackupABExample
{
private const string BackupExtension = ".b";
public static string SaveBackupPath(string path)
{
string altPath = GetAlternativeFilePath(path, BackupExtension);
return GetOldestFilePath(path, altPath);
}
public static string LoadBackupPath(string path)
{
string altPath = GetAlternativeFilePath(path, BackupExtension);
return GetNewestFilePath(path, altPath);
}
private static string GetOldestFilePath(string pathOne, string pathTwo)
{
bool pathOneExists = File.Exists(pathOne);
bool pathTwoExists = File.Exists(pathTwo);
if (!pathOneExists)
return pathOne;
if (!pathTwoExists)
return pathTwo;
DateTime pathOneWriteTime = File.GetLastWriteTime(pathOne);
DateTime pathTwoWriteTime = File.GetLastWriteTime(pathTwo);
return pathOneWriteTime < pathTwoWriteTime ? pathOne : pathTwo;
}
private static string GetNewestFilePath(string pathOne, string pathTwo)
{
bool pathOneExists = File.Exists(pathOne);
bool pathTwoExists = File.Exists(pathTwo);
if (!pathOneExists && !pathTwoExists)
return "";
if (pathOneExists && !pathTwoExists)
return pathOne;
if (!pathOneExists)
return pathTwo;
DateTime pathOneWriteTime = File.GetLastWriteTime(pathOne);
DateTime pathTwoWriteTime = File.GetLastWriteTime(pathTwo);
return pathOneWriteTime > pathTwoWriteTime ? pathOne : pathTwo;
}
private static string GetAlternativeFilePath(string path, string extensionName)
{
return new StringBuilder()
.Append(path)
.Append(extensionName)
.ToString();
}
}