-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
executable file
·183 lines (165 loc) · 6.57 KB
/
MainWindow.xaml.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Serialization;
using Path = System.IO.Path;
using System.Windows.Threading;
namespace DiskSnapshot
{
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
RenderOptions.SetBitmapScalingMode(this, BitmapScalingMode.HighQuality);
HeartbeatTimer = new DispatcherTimer {Interval = TimeSpan.FromSeconds(1.0)};
HeartbeatTimer.Tick += HeartbeatTimer_Tick;
AvailableDrives = System.IO.DriveInfo.GetDrives().Where(d => (d.DriveType != DriveType.NoRootDirectory)).ToArray();
var bootdrive = AvailableDrives.First(x => x.Name.ToUpper() == "C:\\"); // hack
LoadDirectoryStructure(bootdrive);
}
public static readonly DependencyProperty DirectoryStructureProperty =
DependencyProperty.Register("DirectoryStructure", typeof(DirectoryStructure), typeof(MainWindow),
new FrameworkPropertyMetadata());
public DirectoryStructure DirectoryStructure
{
get { return (DirectoryStructure)GetValue(DirectoryStructureProperty); }
set { SetValue(DirectoryStructureProperty, value); }
}
public static readonly DependencyProperty IsSelectingDriveProperty =
DependencyProperty.Register("IsSelectingDrive", typeof(bool), typeof(MainWindow),
new FrameworkPropertyMetadata(false));
public bool IsSelectingDrive
{
get { return (bool)GetValue(IsSelectingDriveProperty); }
set { SetValue(IsSelectingDriveProperty, value); }
}
public static readonly DependencyProperty AvailableDrivesProperty =
DependencyProperty.Register("AvailableDrives", typeof(DriveInfo[]), typeof(MainWindow),
new FrameworkPropertyMetadata());
public DriveInfo[] AvailableDrives
{
get { return (DriveInfo[])GetValue(AvailableDrivesProperty); }
set { SetValue(AvailableDrivesProperty, value); }
}
public static readonly DependencyProperty WorkingProperty =
DependencyProperty.Register("Working", typeof(bool), typeof(MainWindow),
new FrameworkPropertyMetadata(false));
public bool Working
{
get { return (bool)GetValue(WorkingProperty); }
set { SetValue(WorkingProperty, value); }
}
public static readonly DependencyProperty ProgressProperty =
DependencyProperty.Register("Progress", typeof(string), typeof(MainWindow),
new FrameworkPropertyMetadata(""));
public string Progress
{
get { return (string)GetValue(ProgressProperty); }
set { SetValue(ProgressProperty, value); }
}
public static readonly DependencyProperty ProgressDirectoryProperty =
DependencyProperty.Register("ProgressDirectory", typeof(string), typeof(MainWindow),
new FrameworkPropertyMetadata(""));
public string ProgressDirectory
{
get { return (string)GetValue(ProgressDirectoryProperty); }
set { SetValue(ProgressDirectoryProperty, value); }
}
DateTime StartTime;
void HeartbeatTimer_Tick(object sender, EventArgs e)
{
TimeSpan runningTime = DateTime.Now - StartTime;
if (DirectoryStructure.LastScanTimeInSeconds > 0)
{
Progress = string.Format("{0} (ETC {1})", runningTime.ToString("mm\\:ss"),
(TimeSpan.FromSeconds(DirectoryStructure.LastScanTimeInSeconds) - runningTime).ToString("mm\\:ss"));
}
else
{
Progress = runningTime.ToString("mm\\:ss");
}
}
readonly DispatcherTimer HeartbeatTimer;
public const string ShortAlphaNumericProductName = "DiskSnapshot";
public const string CompanyName = "Beckman Coulter";
/// <summary> This is where the database and incident logs reside. </summary>
public static string BaseAppDataDir
{
get
{
// ProgramData/Beckman Coulter/DiskSnapshot
string appDataDir = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
return System.IO.Path.Combine(System.IO.Path.Combine(appDataDir, CompanyName), ShortAlphaNumericProductName);
}
}
void LoadDirectoryStructure(DriveInfo drive)
{
LoadDirectoryStructure(drive.Name.Substring(0,1), drive.Name);
}
void LoadDirectoryStructure(string drive, string rootDirectory)
{
DirectoryStructure ds;
string savedFile = System.IO.Path.Combine(BaseAppDataDir, string.Format("{0}.snapshot", drive));
if (System.IO.File.Exists(savedFile))
{
var s = new XmlSerializer(typeof (DirectoryStructure));
using (var stream = System.IO.File.OpenRead(savedFile))
ds = (DirectoryStructure) s.Deserialize(stream);
}
else
{
ds = new DirectoryStructure(drive, rootDirectory);
}
DirectoryStructure = ds;
}
private void BtnSave_Click(object sender, RoutedEventArgs e)
{
DirectoryStructure.Root.Snapshot();
string savedFile = System.IO.Path.Combine(BaseAppDataDir, string.Format("{0}.snapshot", this.DirectoryStructure.Drive));
System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(savedFile));
var s = new XmlSerializer(typeof(DirectoryStructure));
using (var stream = System.IO.File.Create(savedFile))
s.Serialize(stream, DirectoryStructure);
MessageBox.Show(this, "Saved.", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
}
private void BtnSelectDrive_Click(object sender, RoutedEventArgs e)
{
IsSelectingDrive = true;
}
private void BtnOKDrive_Click(object sender, RoutedEventArgs e)
{
var drive = CbxDrives.SelectedItem as DriveInfo;
if (drive != null)
LoadDirectoryStructure(drive);
IsSelectingDrive = false;
}
private void BtnCancelDrive_Click(object sender, RoutedEventArgs e)
{
IsSelectingDrive = false;
}
private void BtnScan_Click(object sender, RoutedEventArgs e)
{
Working = true;
HeartbeatTimer.Start();
StartTime = DateTime.Now;
Scanner.Refresh(DirectoryStructure, dir => ProgressDirectory = dir, () =>
{
HeartbeatTimer.Stop();
DirectoryStructure.Update();
Working = false;
});
}
}
}