-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
114 lines (101 loc) · 4.42 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
namespace SimpleGameSaveBackuper
{
public partial class Form1 : Form
{
string source = "Source Path", destination = "Destination Path";
string[] folder;
int interval = 20;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = Settings1.Default["source_cfg"].ToString();
source = Settings1.Default["source_cfg"].ToString();
textBox2.Text = Settings1.Default["destination_cfg"].ToString();
destination = Settings1.Default["destination_cfg"].ToString();
textBox3.Text = Settings1.Default["interval_cfg"].ToString();
//don't look at line below XD garbage code inbound
interval = Convert.ToInt32(Settings1.Default["interval_cfg"].ToString());
timer1.Enabled = true;
timer1.Interval = interval * 60000;
}
private void button1_Click(object sender, EventArgs e)
{
source = textBox1.Text;
destination = textBox2.Text;
interval = Convert.ToInt32(textBox3.Text);
Settings1.Default["interval_cfg"] = interval;
Settings1.Default.Save();
timer1.Interval = interval * 60000;
label4.Text = "Settings Set! Interval: " + interval + " minutes -> " + interval * 60000 + " ms";
}
public static string OpenFolderDialog()
{
using (var dlg = new FolderBrowserDialog())
{
if (dlg.ShowDialog() == DialogResult.OK)
return dlg.SelectedPath;
else
return string.Empty;
}
}
private void button2_Click(object sender, EventArgs e)
{
source = OpenFolderDialog();
textBox1.Text = source;
Settings1.Default["source_cfg"] = source;
Settings1.Default.Save();
}
private void button3_Click(object sender, EventArgs e)
{
destination = OpenFolderDialog();
textBox2.Text = destination;
Settings1.Default["destination_cfg"] = destination;
Settings1.Default.Save();
}
private void timer1_Tick(object sender, EventArgs e)
{
label5.Text = "";
CopyFilesRecursively(source, destination + @"\" + folder_slice(source) + " - " + DateTime.Now.ToString().Replace(':', '_'));
label4.Text = "Last backup: " + DateTime.Now.ToString();
}
//I'm pretty sure that function below and folder_slice(source) can be avoided or done better - but guess it's good enough - hey it just works!
public static string folder_slice(string path)
{
string[] folder = path.Split('\\');
return folder[folder.Length - 1];
}
//Stack Overflow function below + try catch for error
private void CopyFilesRecursively(string sourcePath, string targetPath)
{
try
{
//Now Create all of the directories
foreach (string dirPath in Directory.GetDirectories(sourcePath, "*", SearchOption.AllDirectories))
{
Directory.CreateDirectory(dirPath.Replace(sourcePath, targetPath));
}
//Copy all the files & Replaces any files with the same name
foreach (string newPath in Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories))
{
File.Copy(newPath, newPath.Replace(sourcePath, targetPath), true);
}
}
catch (Exception ex)
{
label5.Text = "Exception - check 'Exception_date_time.txt' for more informations. | Usually the file/s that program tried to copy got deleted/renamed during backup or the PATH is incorrect.";
StreamWriter File = new StreamWriter("Exception_" + DateTime.Now.ToString("yyyyMMdd_hhmmss") + ".txt");
File.WriteLine(ex.ToString());
File.Close();
}
}
private void button4_Click(object sender, EventArgs e)
{
label5.Text = "";
CopyFilesRecursively(source, destination + @"\" + folder_slice(source) + " - " + DateTime.Now.ToString().Replace(':', '_'));
label4.Text = "Last backup (MANUAL): " + DateTime.Now.ToString();
}
}
}