-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrmMain.cs
More file actions
833 lines (740 loc) · 32.8 KB
/
frmMain.cs
File metadata and controls
833 lines (740 loc) · 32.8 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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualBasic;
using FileSystem = Microsoft.VisualBasic.FileIO.FileSystem;
namespace IntegralHashing
{
/// <summary>
/// Author: Branden Prins
/// Date: 12:31 AM 6/30/2021
/// Course: 21/SU-COP-2939-53251
/// Program: Computer Programming Capstone
///
/// Integral Hashing is a hash generator that is intended to
/// focus on two algorithms, SHA-256 and SHA-512. The purpose
/// behind these two algorithms is the absence of collision
/// present today. Although, no avoidable, there are no known
/// collisions with SHA-256 or SHA-512. Additionally the
/// program allows users to create hashlists as well as
/// compare the generated hash. The advantage of Integral
/// Hashing is like most open-source programs, it is
/// free-to-use and the features it offers may be of a
/// convenience to others.Users can generate hash lists from
/// any directory, so long as a directory has file contents
/// and data within each file.A unique hash will be generated
/// and users can export this data to a spreadsheet, or textfile
/// as well as compare hashes from a pregenerated, preformatted
/// hash list.
/// </summary>
public partial class frmMain : Form
{
/// <summary>
/// Store dictionary for comparing files and hashes later
/// </summary>
StringStorage storage = new StringStorage();
DirectoryInfo dinfo;
ListViewItem lvi;
FileInfo[] finfo;
/// <summary>
/// Helps to calculator the progress from the stream during the hashing process.
/// </summary>
byte[] buffer;
int bytesRead;
long size;
long tBytesRead = 0;
public frmMain()
{
InitializeComponent();
}
private void mnuExport_Click(object sender, EventArgs e)
{
// Configure SFD
sfdExport = new SaveFileDialog()
{
Filter = "Excel Workbook (*.xlsx)|*.xlsx|Text (MS-DOS) (*.txt)|*.txt",
FilterIndex = 1,
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
RestoreDirectory = true
};
// Make dialog, child to frmMain
if (sfdExport.ShowDialog() == DialogResult.OK)
{
// Call Export Class
teExport tee = new teExport();
try
{
// Check Extension
switch (Path.GetExtension(sfdExport.FileName).ToLower())
{
// If Excel
case ".xlsx":
// Set row/column
int row = 1, column = 1;
/// <summary>
/// Open Worksheet, Export Data, Save Workbook, Close Application
/// </summary>
tee.OpenExcelWorksSheet();
tee.ExportHashColumnsCells(row, column, lstvOutput);
if (!File.Exists(sfdExport.FileName))
{
tee.SaveExcelWorkbook(sfdExport.FileName);
}
else
tee.SaveExcelWorkbook(sfdExport.FileName);
break;
case ".txt":
/// <summary>
/// Save Text Document
/// </summary>
if (!File.Exists(sfdExport.FileName))
{
tee.SaveFileDocument(sfdExport.FileName, tee.ExportText(lstvOutput));
}
else
tee.SaveFileDocument(sfdExport.FileName, tee.ExportText(lstvOutput));
break;
default:
// Throw if filetype not supported
throw new ArgumentOutOfRangeException(Path.GetExtension(sfdExport.FileName).ToLower());
}
}
catch (Exception ioe)
{
MessageBox.Show("File not saved", "Save File", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
/// <summary>
/// Re-useable between controls, cleans things up
/// </summary>
public void ResetControls()
{
// Clear listview items
lstvOutput.Items.Clear();
storage.absolute.Clear();
storage.hashed.Clear();
// Disable context menu options
ctxCopy.Enabled = false;
ctxRename.Enabled = false;
ctxMove.Enabled = false;
ctxRecycle.Enabled = false;
ctxPerma.Enabled = false;
ctxCompareList.Enabled = false;
// Enable Start
btnStartHash.Enabled = true;
// Labels
lblMatched.Text = string.Empty;
lblUnique.Text = string.Empty;
}
/// <summary>
/// Get folder contents from drag and drop
/// </summary>
private void lblDragDrop_Click(object sender, EventArgs e)
{
ResetControls();
if (fbdWorkingPath.ShowDialog() == DialogResult.OK)
{
int count = 1;
GetFolderContents(count, fbdWorkingPath.SelectedPath);
}
}
/// <summary>
/// Get folder contents
/// </summary>
public void GetFolderContents(int count, string path)
{
storage.directory = path;
dinfo = new DirectoryInfo(storage.directory);
finfo = dinfo.GetFiles();
lstvOutput.SuspendLayout();
foreach (FileInfo f in finfo)
{
storage.absolute.Add(f.FullName);
lvi = lstvOutput.Items.Add((count++).ToString());
lvi.ForeColor = Color.Cyan;
lvi.UseItemStyleForSubItems = false;
lvi.SubItems.Add(f.Name);
lvi.SubItems.Add(GetFileSize(f.Length));
lvi.SubItems[2].ForeColor = Color.Magenta;
}
lstvOutput.ResumeLayout();
}
private void pnlDragDrop_DragEnter(object sender, DragEventArgs e)
{
/// <summary>
/// Allow drop for label when entering panel on drag and drop, this prevent clicking
/// the label for folder browser dialog.
/// </summary>
lblDragDrop.AllowDrop = true;
/// <summary>
/// This will determine if the drag source will be copied or ignored
/// upon entry.
/// </summary>
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void pnlDragDrop_DragDrop(object sender, DragEventArgs e)
{
ResetControls();
/// <summary>
/// Disable drop for label when drop complete, this allows clicking
/// the label for folder browser dialog.
/// </summary>
lblDragDrop.AllowDrop = false;
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
/// <summary>
/// When a folder is dropped, GetData will determine if one or more folders are dropped
/// After the directory check returns true, textbox retrieves the folder path
/// </summary>
string[] fldr = (string[])e.Data.GetData(DataFormats.FileDrop);
if (Directory.Exists(fldr[0]))
{
/// <summary>
/// Clear items before dumping files and info
/// </summary>
lstvOutput.Items.Clear();
int count = 1;
GetFolderContents(count, fldr[0]);
}
}
}
private void lblDragDrop_DragEnter(object sender, DragEventArgs e)
{
/// <summary>
/// This will determine if the drag source will be copied or ignored
/// upon entry.
/// </summary>
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void lblDragDrop_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
/// <summary>
/// When a folder is dropped, GetData will determine if one or more folders are dropped
/// After the directory check returns true, textbox retrieves the folder path
/// </summary>
string[] fldr = (string[])e.Data.GetData(DataFormats.FileDrop);
/// <summary>
/// Clear items before dumping files and info
/// </summary>
lstvOutput.Items.Clear();
if (Directory.Exists(fldr[0]))
{
storage.directory = fldr[0];
bgwHashToListView.RunWorkerAsync();
}
}
}
private void mnuExit_Click(object sender, EventArgs e)
{
this.Close(); // Terminate the application
}
public string GetFileSize(double length)
{
/// <summary>
/// This will determine the file size for each file being processed
/// Highest support measurement Terabyte
/// Anything less than 1 KB will report as KB
/// </summary>
string format = string.Empty;
if (length < 1024f)
format = "1 KB";
if (length > 1024f && length <= Math.Pow(1024f, 2))
format = $"{Math.Round(length / 1024.0)} KB";
if (length > Math.Pow(1024f, 2) && length <= Math.Pow(1024f, 3))
format = $"{Math.Round(length / Math.Pow(1024, 2))} MB";
if (length > Math.Pow(1024f, 3) && length <= Math.Pow(1024f, 4))
format = $"{Math.Round(length / Math.Pow(1024, 3))} GB";
if (length > Math.Pow(1024f, 4) && length <= Math.Pow(1024f, 5))
format = $"{Math.Round(length / Math.Pow(1024, 4))} TB";
return format;
}
/// <summary>
/// Use label as click option to open file browser dialog
/// </summary>
private void lblDragDrop1_Click(object sender, EventArgs e)
{
ofdImport = new OpenFileDialog()
{
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
RestoreDirectory = true,
FilterIndex = 1,
Filter = "Text (MS-DOS) (*.txt)|*.txt"
};
if (ofdImport.ShowDialog() == DialogResult.OK)
{
TextImport(ofdImport.FileName);
}
}
private void lblDragDrop1_DragEnter(object sender, DragEventArgs e)
{
/// <summary>
/// This will determine if the drag source will be copied or ignored
/// upon entry.
/// </summary>
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void lblDragDrop1_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
/// <summary>
/// When a folder is dropped, GetData will determine if one or more folders are dropped
/// After the directory check returns true, textbox retrieves the folder path
/// </summary>
string[] file = (string[])e.Data.GetData(DataFormats.FileDrop);
if (File.Exists(file[0]))
{
/// <summary>
/// Open file dialog never opens, update name
/// </summary>
TextImport(file[0]);
}
}
}
public void TextImport(string filename)
{
/// <summary>
/// conver all lines in file to array, then clean array
/// before dictionary conversion
/// </summary>
string[] hashes = File.ReadAllText(filename).Replace("\r", string.Empty).Split('\n');
hashes = hashes.Where(x => !string.IsNullOrEmpty(x)).ToArray();
// Initialize new keyvalue storage
storage.keyVal = new Dictionary<string, string>();
// split arrays at ',' then store key+value
foreach (string h in hashes)
{
var splt = h.Split(',');
if (!string.IsNullOrEmpty(splt[0]) && !string.IsNullOrEmpty(splt[1]))
{
// Do not store if already exists
if (!storage.keyVal.ContainsKey(splt[0]))
{
storage.keyVal.Add(splt[0], splt[1]);
}
}
}
/// <summary>
/// File and hash count, in event that a key is missing a value
/// </summary>
lblFileCount.Text = storage.keyVal.Keys.Count.ToString();
if (storage.hashed.Count != 0)
{
ctxCompareList.Enabled = true;
}
}
private void pnlDragDrop1_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
/// <summary>
/// When a folder is dropped, GetData will determine if one or more folders are dropped
/// After the directory check returns true, textbox retrieves the folder path
/// </summary>
string[] file = (string[])e.Data.GetData(DataFormats.FileDrop);
if (File.Exists(file[0]))
{
/// <summary>
/// Open file dialog never opens, update name
/// </summary>
TextImport(file[0]);
}
}
}
private void pnlDragDrop1_DragEnter(object sender, DragEventArgs e)
{
/// <summary>
/// This will determine if the drag source will be copied or ignored
/// upon entry.
/// </summary>
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void mnuAbout_Click(object sender, EventArgs e)
{
About about = new About();
about.ShowDialog();
}
/// <summary>
/// Convert byte array to human readable format using linq then return.
/// </summary>
public string ByteToStringConversion(byte[] bytes)
{
StringBuilder r = new StringBuilder();
foreach (byte b in bytes)
{
r.Append(b.ToString("x2"));
}
return r.ToString();
}
private void btnStartHash_Click(object sender, EventArgs e)
{
// Run one of two threads.
switch(cmbAlgorithm.SelectedIndex)
{
case 1:
bgwSha256.RunWorkerAsync();
break;
case 2:
bgwSha512.RunWorkerAsync();
break;
}
// Enable context menu items for editing.
EnableContextMenuItems();
}
// Function to enable necessary context menu items
public void EnableContextMenuItems()
{
ctxCopy.Enabled = true;
ctxRename.Enabled = true;
ctxMove.Enabled = true;
ctxRecycle.Enabled = true;
ctxPerma.Enabled = true;
ctxFileLocation.Enabled = true;
}
/// <summary>
/// Background worker for SHA256
/// </summary>
private void bgwSha256_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
storage.hashed.Clear(); // Cleared stored hashes, helps with drawing correct values in listview
for (int count = 0; count < storage.absolute.Count; count++)
{
lblWorkingFileName.Text = storage.absolute[count];
if (File.Exists(storage.absolute[count]))
{
size = new FileInfo(storage.absolute[count]).Length; // Report progress of hashing process through the file size.
// Prevents streaming files with zero data
if (size != 0)
{
using (FileStream fStream = File.OpenRead(storage.absolute[count]))
{
using (SHA256 s256 = SHA256.Create())
{
do
{
// Only used for progress
buffer = new byte[4096];
bytesRead = fStream.Read(buffer, 0, buffer.Length);
tBytesRead += bytesRead;
s256.TransformBlock(buffer, 0, bytesRead, null, 0);
bgwSha256.ReportProgress((int)(((double)tBytesRead / size) * 100));
} while (bytesRead != 0);
// Reset progress
tBytesRead = 0;
// Convert string add to storage
storage.hashed.Add(ByteToStringConversion(s256.ComputeHash(fStream)));
}
}
}
else
// Add no data if hash missing or no data within file, prevents exception
storage.hashed.Add("(no data)");
}
}
// Run background worker
bgwHashToListView.RunWorkerAsync();
}
private void bgwSha256_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
// Update value of progress bar
progressBar1.Value = e.ProgressPercentage;
}
private void bgwSha256_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
MessageBox.Show("SHA-256 Hashing Complete!", "Hashing Complete", MessageBoxButtons.OK, MessageBoxIcon.Information);
progressBar1.Value = 0;
lblWorkingFileName.Text = string.Empty;
}
/// <summary>
/// Background worker for SHA512
/// </summary>
private void bgwSha512_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
storage.hashed.Clear(); // Cleared stored hashes, helps with drawing correct values in listview
for (int count = 0; count < storage.absolute.Count; count++)
{
lblWorkingFileName.Text = storage.absolute[count];
if (File.Exists(storage.absolute[count]))
{
size = new FileInfo(storage.absolute[count]).Length; // Report progress of hashing process through the file size.
// Files MUST have data in order for hash to be created successfully!
if (size != 0)
{
using (FileStream fStream = File.OpenRead(storage.absolute[count]))
{
using (SHA512 s512 = SHA512.Create())
{
do
{
// Only used for progress
buffer = new byte[4096];
bytesRead = fStream.Read(buffer, 0, buffer.Length);
tBytesRead += bytesRead;
s512.TransformBlock(buffer, 0, bytesRead, null, 0);
bgwSha512.ReportProgress((int)(((double)tBytesRead / size) * 100));
} while (bytesRead != 0);
// Reset progress
tBytesRead = 0;
// Convert string add to storage
storage.hashed.Add(ByteToStringConversion(s512.ComputeHash(fStream)));
}
}
}
else
// Add no data if hash missing or no data within file, prevents exception
storage.hashed.Add("(no data)");
}
}
// Run background worker
bgwHashToListView.RunWorkerAsync();
}
private void bgwSha512_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage; // Update value of progress bar
}
private void bgwSha512_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
MessageBox.Show("SHA-512 Hashing Complete!", "Hashing Complete", MessageBoxButtons.OK, MessageBoxIcon.Information);
progressBar1.Value = 0;
lblWorkingFileName.Text = string.Empty;
}
private void bgwHashToListView_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
/// <summary>
/// Invoke ot new thread, avoid cross-thread exception
/// </summary>
if (lstvOutput.InvokeRequired)
{
lstvOutput.Invoke((MethodInvoker)delegate ()
{
// Probably pointless to use
lstvOutput.SuspendLayout();
int count = 0;
foreach (ListViewItem i in lstvOutput.Items)
{
/// <summary>
/// Checks if listview already contains subitems to update
/// values with different checksum
/// </summary>
if (i.SubItems.Count != lstvOutput.Columns.Count)
{
i.SubItems.Add(storage.hashed[count]);
i.SubItems[3].ForeColor = Color.Yellow;
count++;
}
else
{
i.SubItems[3].Text = storage.hashed[count];
i.SubItems[3].ForeColor = Color.Yellow;
count++;
}
}
// Probably pointless to use
lstvOutput.ResumeLayout();
});
}
}
private void lstvOutput_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
ctxIdentifier.Text = $"ID: {lstvOutput.SelectedItems[0].Text}";
}
/// <summary>
/// Nullify exception, ArgumentOutOfRangeException thrown if index selected but right click elsewhere.
/// Nullifying exception does not break, instead, right clicking will change the selected index providing context
/// </summary>
catch { }
}
private void ctxExit_Click(object sender, EventArgs e)
{
/// <summary>
/// Prompt Exit if list view contains items
/// </summary>
if (lstvOutput.Items.Count > 0)
{
DialogResult result = MessageBox.Show("Are you sure you want to exit?","Exit Application",MessageBoxButtons.YesNo,MessageBoxIcon.Warning);
if (result == DialogResult.Yes)
{
this.Close();
}
}
else
// Exit if no items
this.Close();
}
private void ctxCopy_Click(object sender, EventArgs e)
{
// Copy selected hash text from 4th column, to clipboard.
Clipboard.SetText(lstvOutput.SelectedItems[0].SubItems[3].Text);
}
private void ctxRename_Click(object sender, EventArgs e)
{
try
{
/// <summary>
/// This will convert the select index form listview to identify the respect absolute path in the array
/// Then once the method is complete, the value is color coded to easier identify changes from the results
/// </summary>
if (Int32.TryParse(lstvOutput.SelectedItems[0].Text, out int index))
{
lstvOutput.SelectedItems[0].SubItems[1].Text = $"{RenameMoveFile(index, "")}";
lstvOutput.SelectedItems[0].SubItems[1].ForeColor = Color.White;
}
}
catch (IOException fex)
{
MessageBox.Show($"{fex.Message}");
}
}
private void ctxMove_Click(object sender, EventArgs e)
{
try
{
/// <summary>
/// This will convert the select index form listview to identify the respect absolute path in the array
/// Then once the method is complete, the value is color coded to easier identify changes from the results
/// </summary>
if (Int32.TryParse(lstvOutput.SelectedItems[0].Text, out int index))
{
fbdWorkingPath.ShowDialog();
RenameMoveFile(index, fbdWorkingPath.SelectedPath);
lstvOutput.SelectedItems[0].SubItems[1].ForeColor = Color.FromArgb(0, 255, 130, 36);
}
}
catch (IOException fex)
{
MessageBox.Show($"{fex.Message}");
}
}
public string RenameMoveFile(int index, string newpath)
{
/// <summary>
/// Break absolute path apart and re-assign as needed
/// </summary>
string directory = Path.GetDirectoryName($"{storage.absolute[index - 1]}");
string filename = Path.GetFileNameWithoutExtension($"{ storage.absolute[index - 1]}");
string extension = Path.GetExtension($"{storage.absolute[index - 1]}");
string nFilename = string.Empty;
if (!string.IsNullOrEmpty(newpath))
{
/// <summary>
/// Moves the file to a different directory
/// </summary>
File.Move($"{storage.absolute[index - 1]}", $"{newpath}\\{filename}{extension}");
storage.absolute[index - 1] = $"{newpath}\\{filename}{extension}";
}
else
{
/// <summary>
/// Renames the file in the given directory
/// </summary>
nFilename = Interaction.InputBox("Filename: ", "Rename File", $"{lstvOutput.SelectedItems[0].SubItems[1].Text}");
File.Move($"{storage.absolute[index - 1]}", $"{directory}\\{nFilename}{extension}");
storage.absolute[index - 1] = $"{directory}\\{nFilename}{extension}";
}
// Return filename to update listview
return $"{nFilename}{extension}";
}
private void ctxFileLocation_Click(object sender, EventArgs e)
{
if (Int32.TryParse(lstvOutput.SelectedItems[0].Text, out int index))
{
/// <summary>
/// Assigns a parameter with the given path, open and selects the file within explorer.
/// </summary>
string parameter = $"/select,{storage.absolute[index - 1]}";
Process.Start("explorer.exe", parameter);
}
}
private void ctxRecycle_Click(object sender, EventArgs e)
{
if (Int32.TryParse(lstvOutput.SelectedItems[0].Text, out int index))
{
FileSystem.DeleteFile($"{storage.absolute[index - 1]}", Microsoft.VisualBasic.FileIO.UIOption.AllDialogs, Microsoft.VisualBasic.FileIO.RecycleOption.SendToRecycleBin,Microsoft.VisualBasic.FileIO.UICancelOption.ThrowException);
lstvOutput.SelectedItems[0].SubItems[1].ForeColor = Color.FromArgb(0, 58, 58, 58);
}
// Prevents rehashing in case files not present, results in program crashing
btnStartHash.Enabled = false;
}
private void ctxPerma_Click(object sender, EventArgs e)
{
if (Int32.TryParse(lstvOutput.SelectedItems[0].Text, out int index))
{
FileSystem.DeleteFile($"{storage.absolute[index - 1]}", Microsoft.VisualBasic.FileIO.UIOption.AllDialogs, Microsoft.VisualBasic.FileIO.RecycleOption.DeletePermanently, Microsoft.VisualBasic.FileIO.UICancelOption.ThrowException);
lstvOutput.SelectedItems[0].SubItems[1].ForeColor = Color.Red;
}
// Prevents rehashing in case files not present, results in program crashing
btnStartHash.Enabled = false;
}
private void ctxCompareList_Click(object sender, EventArgs e)
{
// Creates new instance for storing and counting compared later
storage.matched = new Dictionary<string, string>();
storage.unique = new Dictionary<string, string>();
// For each subitem in output check if dictionary contains the item
foreach (ListViewItem i in lstvOutput.Items)
{
if (storage.keyVal.ContainsValue(i.SubItems[3].Text))
{
// Color pale Green if found
i.SubItems[3].ForeColor = Color.FromArgb(0, 128, 255, 128);
storage.matched.Add(i.SubItems[1].Text, i.SubItems[3].Text);
}
else
{
// Color pale Red if missing
i.SubItems[3].ForeColor = Color.FromArgb(0, 255, 192, 192);
storage.unique.Add(i.SubItems[1].Text, i.SubItems[3].Text);
}
}
// Update labels with count on matching and unique
lblMatched.Text = $"{storage.matched.Keys.Count}";
lblUnique.Text = $"{storage.unique.Keys.Count}";
}
// UserManual private, unchanged
private const string chmFileName = "UserManual.chm";
private void mnuSupport_Click(object sender, EventArgs e)
{
// Show help when support clicked
Help.ShowHelp(this, Application.StartupPath + $"\\Resources\\{chmFileName}");
}
}
}