-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainForm.cs
More file actions
385 lines (342 loc) · 12.4 KB
/
MainForm.cs
File metadata and controls
385 lines (342 loc) · 12.4 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
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
using Microsoft.Collections.Extensions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DupeFinder
{
public partial class MainForm : Form
{
DataTable dataTable = new DataTable("Dupe Data");
int count;
int num_done;
string saveFile;
public MainForm()
{
InitializeComponent();
var nameCol = dataTable.Columns.Add("Name", typeof(string));
var sizeCol = dataTable.Columns.Add("Size", typeof(long));
var h1kCol = dataTable.Columns.Add("Hash1k", typeof(string));
var h1mCol = dataTable.Columns.Add("Hash1M", typeof(string));
var hCol = dataTable.Columns.Add("Hash", typeof(string));
dataTable.PrimaryKey = new DataColumn[] { nameCol };
saveFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "DupeFinder.xml");
if (File.Exists(saveFile) && MessageBox.Show("Load saved session?", "Dupe Finder", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
using (var str = File.OpenRead(saveFile))
{
dataTable.ReadXml(str);
}
}
resultsView.DataSource = dataTable;
resultsView.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
resultsView.Columns[1].DefaultCellStyle.Format = "N0";
}
private void start_Click(object sender, EventArgs e)
{
dataTable.Clear();
foreach (var f in Directory.EnumerateFiles(src1.Text, "*", SearchOption.AllDirectories))
{
var fi = new FileInfo(f);
dataTable.Rows.Add(new object[] { f, fi.Length });
}
foreach (var f in Directory.EnumerateFiles(src2.Text, "*", SearchOption.AllDirectories))
{
var fi = new FileInfo(f);
dataTable.Rows.Add(new object[] { f, fi.Length });
}
num_done = 0;
timer1.Tick += SizeDups;
timer1.Start();
}
private void SizeDups(object sender, EventArgs e)
{
timer1.Tick -= SizeDups;
timer1.Stop();
var sizes = new Dictionary<long, List<string>>();
foreach (var row in dataTable.AsEnumerable())
{
string f = row.Field<string>(0);
long s = row.Field<long>(1);
if (sizes.TryGetValue(s, out List<string> fl))
{
fl.Add(f);
}
else
{
sizes.Add(s, new List<string>() { f });
}
}
foreach (var item in sizes)
{
if (item.Value.Count == 1)
{
dataTable.Rows.Find(item.Value.First()).Delete();
}
}
count = dataTable.Rows.Count;
timer1.Tick += DoHash1k;
timer1.Start();
}
private void DoHash1k(object sender, EventArgs e)
{
timer1.Tick -= DoHash1k;
timer1.Stop();
DateTime t = DateTime.Now;
progressBar.Value = 100 * num_done / count;
foreach (var row in dataTable.AsEnumerable().
Where(val => val.Field<string>("Hash1k") == null))
{
using (var br = new BinaryReader(File.OpenRead(row.Field<string>("Name"))))
{
var bytes = br.ReadBytes(10240);
using (SHA256 sha = SHA256.Create())
{
var hash = sha.ComputeHash(bytes);
row.SetField<string>("Hash1k", BitConverter.ToString(hash));
num_done++;
}
}
if ((DateTime.Now -t).TotalMilliseconds > 500)
{
timer1.Tick += DoHash1k;
timer1.Start();
return;
}
}
progressBar.Value = 0;
timer1.Tick += HashDups;
timer1.Start();
}
private Dictionary<string, List<string>> GetDupeList()
{
var hashes = new Dictionary<string, List<string>>();
foreach (var row in dataTable.AsEnumerable())
{
string f = row.Field<string>(0);
string h = row.Field<long?>(1).ToString() + row.Field<string>(2);
if (row.Field<string>(3) != null)
{
h += row.Field<string>(3);
num_done++;
}
if (row.Field<string>(4) != null)
{
h += row.Field<string>(4);
num_done++;
}
if (hashes.TryGetValue(h, out List<string> fl))
{
fl.Add(f);
}
else
{
hashes.Add(h, new List<string>() { f });
}
}
var files = new Dictionary<string, List<string>>();
foreach (var item in hashes)
{
foreach (var f in item.Value)
{
files.Add(f, item.Value.Where(x => x != f).ToList());
}
}
return files;
}
private void HashDups()
{
var hashes = new Dictionary<string, List<string>>();
foreach (var row in dataTable.AsEnumerable())
{
string f = row.Field<string>(0);
string h = row.Field<long?>(1).ToString() + row.Field<string>(2);
if (row.Field<string>(3) != null)
{
h += row.Field<string>(3);
num_done++;
}
if (row.Field<string>(4) != null)
{
h += row.Field<string>(4);
num_done++;
}
if (hashes.TryGetValue(h, out List<string> fl))
{
fl.Add(f);
}
else
{
hashes.Add(h, new List<string>() { f });
}
}
foreach (var item in hashes)
{
if (item.Value.Count == 1)
{
dataTable.Rows.Find(item.Value.First()).Delete();
}
}
}
private void HashDups(object sender, EventArgs e)
{
timer1.Tick -= HashDups;
timer1.Stop();
HashDups();
}
private void DoHash1m(string path)
{
using (var br = new BinaryReader(File.OpenRead(path)))
{
var bytes = br.ReadBytes(1024 * 1024);
using (SHA256 sha = SHA256.Create())
{
var hash = sha.ComputeHash(bytes);
var row = dataTable.Rows.Find(path);
row.SetField<string>("Hash1m", BitConverter.ToString(hash));
}
}
}
private void DoHash(string path)
{
//using (SHA256 sha = SHA256.Create())
//{
// var hash = sha.ComputeHash(File.OpenRead(path));
// var row = dataTable.Rows.Find(path);
// row.SetField<string>("Hash", BitConverter.ToString(hash));
//}
var len = (new FileInfo(path)).Length;
using (var fs = File.OpenRead(path))
{
byte[] buffer = new byte[16 * 1024];
using (SHA256 sha = SHA256.Create())
{
for (int segment = 0; segment < 16; segment++)
{
long pos = len * segment / 16L;
fs.Seek(pos, SeekOrigin.Begin);
fs.Read(buffer, segment * 1024, 1024);
}
var hash = sha.ComputeHash(buffer);
var row = dataTable.Rows.Find(path);
row.SetField<string>("Hash", BitConverter.ToString(hash));
}
}
}
private void hash1MToolStripMenuItem_Click(object sender, EventArgs e)
{
var hashes = GetDupeList();
foreach (var s in resultsView.SelectedRows)
{
var row = ((s as DataGridViewRow).DataBoundItem as DataRowView).Row;
string f = row.Field<string>(0);
DoHash1m(f);
if (hashes.TryGetValue(f, out var others))
{
foreach (var x in others)
{
DoHash1m(x);
}
}
}
HashDups();
}
private void hashToolStripMenuItem_Click(object sender, EventArgs e)
{
var hashes = GetDupeList();
progressBar.Value = 0;
int c = resultsView.SelectedRows.Count;
int n = 0;
var toHash = new HashSet<string>();
foreach (var s in resultsView.SelectedRows)
{
var row = ((s as DataGridViewRow).DataBoundItem as DataRowView).Row;
string f = row.Field<string>(0);
toHash.Add(f);
}
foreach (var f in toHash)
{
DoHash(f);
if (hashes.TryGetValue(f, out var others))
{
foreach (var x in others)
{
if (!toHash.Contains(x))
DoHash(x);
}
}
n++;
progressBar.Value = 100 * n / c;
}
progressBar.Value = 0;
timer1.Tick += HashDups;
timer1.Start();
}
private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
progressBar.Value = 0;
int c = resultsView.SelectedRows.Count;
int n = 0;
foreach (var s in resultsView.SelectedRows)
{
var row = ((s as DataGridViewRow).DataBoundItem as DataRowView).Row;
Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile(row.Field<string>(0),
Microsoft.VisualBasic.FileIO.UIOption.OnlyErrorDialogs,
Microsoft.VisualBasic.FileIO.RecycleOption.SendToRecycleBin);
row.Delete();
n++;
progressBar.Value = 100 * n / c;
}
progressBar.Value = 0;
timer1.Tick += HashDups;
timer1.Start();
}
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
var row = (resultsView.CurrentRow.DataBoundItem as DataRowView).Row;
using (Process proc = new Process())
{
proc.StartInfo.FileName = row.Field<string>(0);
proc.StartInfo.UseShellExecute = true;
proc.Start();
}
}
private void saveButton_Click(object sender, EventArgs e)
{
using (var str = File.Create(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "DupeFinder.xml")))
{
dataTable.WriteXml(str);
}
}
private void candidateButton_Click(object sender, EventArgs e)
{
resultsView.ClearSelection();
var files = GetDupeList();
foreach (var s in resultsView.Rows)
{
var dgvr = s as DataGridViewRow;
var row = (dgvr.DataBoundItem as DataRowView).Row;
var f = row.Field<string>(0);
if (f.StartsWith(src1.Text))
{
if (files.TryGetValue(f, out var others))
{
if (others.Count == 1 &&
others.First().StartsWith(src2.Text))
{
dgvr.Selected = true;
}
}
}
}
}
}
}