-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
1389 lines (1098 loc) · 50.4 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
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
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Threading.Tasks;
using Microsoft.Win32;
using System.Collections.Concurrent;
using LiveCharts.Wpf;
using LiveCharts;
using System.Windows.Media.Effects;
namespace DiskManager
{
public partial class MainWindow : Window
{
public List<ProgressBarItem> ProgressBarItems { get; set; }
private List<string> foundFiles = new List<string>();
private ListBox displayFiles; // Declare displayFiles ListBox here
private Slider numberSlider; // Declare numberSlider as a private member of the MainWindow class
private TextBlock selectedNumberTextBlock; // Declare selectedNumberTextBlock as a private member of the MainWindow class
private TextBlock fileCountTextBlock; // count Large Files
private TextBlock ResultTextBlock;
private TextBlock folderPathTextBox;
private ComboBox fileTypeComboBox;// Declare the ComboBox as a member variable of the class
private ComboBox duplicateFileTypeComboBox;
private Button browseFolderButton;
private Button browseFileButton;
private Button deleteButton;
private Button findLargeSizedFilesButton; // Large File size button
private double minSize;
private long count = 0;
private string fileFormat = "";
string[] allFiles;
Dictionary<string, double> categorySizes = new Dictionary<string, double>(); // DICTIONARY TO KEEP VALUES IN PIE CHART
private Dictionary<string, List<string>> duplicateFilesMap = new Dictionary<string, List<string>>();
public MainWindow() // intialize all
{
InitializeComponent();
ProgressBarItems = new List<ProgressBarItem>();
createContent();
DataContext = this;
}
public class ProgressBarItem
{
public string Name { get; set; }
public long Progress { get; set; }
public long MaxValue { get; set; }
public string Size { get; set; }
}
private Style CreateRoundButtonStyle()
{
Style roundButtonStyle = new Style(typeof(Button));
// Set the template for the button
roundButtonStyle.Setters.Add(new Setter(Control.TemplateProperty, CreateButtonTemplate()));
// Set other properties for the button
roundButtonStyle.Setters.Add(new Setter(Button.HeightProperty, 40.0)); // Increase height by 100
roundButtonStyle.Setters.Add(new Setter(Button.WidthProperty, 400.0));
roundButtonStyle.Setters.Add(new Setter(Button.PaddingProperty, new Thickness(10)));
roundButtonStyle.Setters.Add(new Setter(Button.MarginProperty, new Thickness(5)));
return roundButtonStyle;
}
private ControlTemplate CreateButtonTemplate()
{
ControlTemplate buttonTemplate = new ControlTemplate(typeof(Button));
// Create the border to represent the button
FrameworkElementFactory borderFactory = new FrameworkElementFactory(typeof(Border));
borderFactory.Name = "roundedRectangle";
// Set the button background color to #FEEADA
borderFactory.SetValue(Border.BackgroundProperty, (SolidColorBrush)(new BrushConverter().ConvertFrom("#FEEADA")));
borderFactory.SetValue(Border.CornerRadiusProperty, new CornerRadius(6.0));
// Create the content presenter to display the button content
FrameworkElementFactory contentPresenterFactory = new FrameworkElementFactory(typeof(ContentPresenter));
contentPresenterFactory.SetValue(ContentPresenter.HorizontalAlignmentProperty, HorizontalAlignment.Center);
contentPresenterFactory.SetValue(ContentPresenter.VerticalAlignmentProperty, VerticalAlignment.Center);
borderFactory.AppendChild(contentPresenterFactory);
// Set the visual tree of the control template
buttonTemplate.VisualTree = borderFactory;
// Define triggers for different button states
Trigger mouseOverTrigger = new Trigger
{
Property = UIElement.IsMouseOverProperty,
Value = true
};
// Set the hover color for the button (light gray)
mouseOverTrigger.Setters.Add(new Setter(Border.BackgroundProperty, new SolidColorBrush(Colors.LightGray)));
buttonTemplate.Triggers.Add(mouseOverTrigger);
return buttonTemplate;
}
void CreateAbout()
{
// Create the StackPanel to hold the content
StackPanel contentStackPanel = new StackPanel
{
Margin = new Thickness(20),
VerticalAlignment = VerticalAlignment.Center
};
// Create the TextBlock to display the instruction
TextBlock Motivation = new TextBlock
{
Text = "MOTIVATION",
FontSize = 22,
FontWeight = FontWeights.ExtraBold,
Margin = new Thickness(5),
HorizontalAlignment = HorizontalAlignment.Center
};
TextBlock Motivation_Text = new TextBlock
{
Text = "This application was developed during a Hackathon organised by Spark under the event Spark August Hackathon,23 ," +
" showcasing rapid development skills. " +
"dedication to creating innovative solutions within a tight timeframe. ",
FontSize = 16,
FontWeight = FontWeights.DemiBold,
Margin = new Thickness(5),
TextWrapping = TextWrapping.Wrap,
TextAlignment = TextAlignment.Center,
};
TextBlock TeamMembers = new TextBlock
{
Text = "\"Brilliantly crafted by innovative minds\"",
FontSize = 22,
FontWeight = FontWeights.ExtraBold,
FontStyle = FontStyles.Italic,
Margin = new Thickness(5, 5, 5, 20),
HorizontalAlignment = HorizontalAlignment.Center
};
contentStackPanel.Children.Add(TeamMembers);
List<Profile> profiles = new List<Profile>
{
new Profile { Name = "Abhishek Mallick", Email = "mallickabhishek97@gmail.com@gmail.com", Position = "Pyhton FullStack Developer" }
};
// Create UI elements dynamically for each profile
foreach (var profile in profiles)
{
StackPanel profileInfoPanel = new StackPanel();
TextBlock nameTextBlock = new TextBlock
{
Text = profile.Name,
FontSize = 16,
FontWeight = FontWeights.DemiBold,
TextWrapping = TextWrapping.Wrap
};
profileInfoPanel.Children.Add(nameTextBlock);
TextBlock emailTextBlock = new TextBlock
{
Text = profile.Email,
FontSize = 10,
TextWrapping = TextWrapping.Wrap
};
profileInfoPanel.Children.Add(emailTextBlock);
TextBlock positionTextBlock = new TextBlock
{
Text = profile.Position,
FontSize = 12,
TextWrapping = TextWrapping.Wrap
};
profileInfoPanel.Children.Add(positionTextBlock);
// Add the profile info panel to the main StackPanel (UI Element)
Border profileBorder = new Border
{
BorderBrush = (SolidColorBrush)(new BrushConverter().ConvertFrom("#33334C")),
BorderThickness = new Thickness(0.2),
Margin = new Thickness(0, 0, 0, 10), // Add some margin between profiles
Padding = new Thickness(5), // Add some padding within the border
Child = profileInfoPanel
};
// Apply box shadow to the Border dynamically
DropShadowEffect dropShadow = new DropShadowEffect
{
BlurRadius = 5,
ShadowDepth = 2,
Color = Colors.Black,
Opacity = 0.3
};
profileBorder.Effect = dropShadow;
contentStackPanel.Children.Add(profileBorder);
}
contentStackPanel.Children.Add(Motivation);
contentStackPanel.Children.Add(Motivation_Text);
// Clear the previous content from the MAIN_AREA StackPanel
MAIN_AREA.Children.Clear();
// Add the content StackPanel to the MAIN_AREA StackPanel
MAIN_AREA.Children.Add(contentStackPanel);
}
private void Button4_Click(object sender, RoutedEventArgs e)
{
MAIN_AREA.Children.Clear();
CreateAbout();
}
// ------------------------------------------------------------- DELETE ------------------------------------------------------------------------------
private void Button3_Click(object sender, RoutedEventArgs e)
{
MAIN_AREA.Children.Clear();
StackPanel deleteFolder = new StackPanel();
folderPathTextBox = new TextBlock
{
Text = "Path : ",
FontSize = 16,
FontWeight = FontWeights.Bold,
};
Border border = new Border
{
BorderThickness = new Thickness(3), // Thicker border (e.g., 3 pixels)
BorderBrush = Brushes.Black,
Margin = new Thickness(5), // Margin of 10 units on all sides
Padding = new Thickness(10), // left top right bottom
Child = folderPathTextBox // Set the StackPanel as the child of the Border
};
browseFolderButton = new Button
{
Margin = new Thickness(10, 50, 10, 10),
Content = "Choose folder"
};
browseFolderButton.Click += BrowseButton_Click;
browseFolderButton.Style = CreateRoundButtonStyle();
deleteFolder.Children.Add(new TextBlock
{
Text = " ",
FontSize = 16,
FontWeight = FontWeights.Bold,
Margin = new Thickness(5)
}
);
browseFileButton = new Button
{
Margin = new Thickness(10, 50, 10, 10),
Content = "Choose Specific File"
};
browseFileButton.Click += BrowseFileButton_Click;
browseFileButton.Style = CreateRoundButtonStyle();
deleteFolder.Children.Add(new TextBlock
{
Text = " ",
FontSize = 16,
FontWeight = FontWeights.Bold,
Margin = new Thickness(5)
}
);
deleteButton = new Button
{
Margin = new Thickness(10, 50, 10, 10),
VerticalAlignment = VerticalAlignment.Bottom,
Content = "Delete"
};
deleteFolder.Children.Add(new TextBlock
{
Text = " ",
FontSize = 16,
FontWeight = FontWeights.Bold,
Margin = new Thickness(5)
}
);
deleteButton.Click += DeleteFolderButton_Click;
deleteButton.Style = CreateRoundButtonStyle();
deleteFolder.Children.Add(border);
deleteFolder.Children.Add(browseFolderButton);
deleteFolder.Children.Add(browseFileButton);
deleteFolder.Children.Add(deleteButton);
MAIN_AREA.Children.Add(deleteFolder);
}
private void BrowseButton_Click(object sender, RoutedEventArgs e)
{
var folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();
if (folderBrowserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string folderPath = folderBrowserDialog.SelectedPath; ;
folderPathTextBox.Text = "Path : " + folderPath;
}
}
private void BrowseFileButton_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog
{
Title = "Select a File",
Filter = "All Files (*.*)|*.*"
};
if (openFileDialog.ShowDialog() == true)
{
string selectedFilePath = openFileDialog.FileName;
folderPathTextBox.Text = "Path : " + selectedFilePath;
MessageBox.Show($"Selected file: {selectedFilePath}");
}
}
private void DeleteFolderButton_Click(object sender, RoutedEventArgs e)
{
string folderPath = folderPathTextBox.Text.Substring(7); ;
if (string.IsNullOrWhiteSpace(folderPath))
{
System.Windows.MessageBox.Show("Please enter a valid folder path or select a file.");
return;
}
try
{
if (Directory.Exists(folderPath))
{
Directory.Delete(folderPath, true);
System.Windows.MessageBox.Show("Folder deleted successfully.");
folderPathTextBox.Text = "Path : ";
}
else if (System.IO.File.Exists(folderPath))
{
System.IO.File.Delete(folderPath);
System.Windows.MessageBox.Show("File deleted successfully.");
folderPathTextBox.Text = "Path : ";
}
else
{
System.Windows.MessageBox.Show("The folder or file does not exist.");
}
}
catch (Exception ex)
{
System.Windows.MessageBox.Show($"An error occurred: {ex.Message}");
}
}
// -------------------------------------------------------- BUTTON 2 (SEARCH) -------------------------------------------------------------
void CreateAndAddContent()
{
// Create the StackPanel to hold the content
StackPanel contentStackPanel = new StackPanel
{
Margin = new Thickness(20),
VerticalAlignment = VerticalAlignment.Center
};
StackPanel searchByFileTypePanel = new StackPanel
{
Margin = new Thickness(20),
VerticalAlignment = VerticalAlignment.Center,
Orientation = Orientation.Horizontal,
HorizontalAlignment = HorizontalAlignment.Center
};
StackPanel duplicateFilePanel = new StackPanel
{
Margin = new Thickness(20),
VerticalAlignment = VerticalAlignment.Center,
Orientation = Orientation.Horizontal,
HorizontalAlignment = HorizontalAlignment.Center
};
// Create the TextBlock to display the instruction
TextBlock instructionTextBlock = new TextBlock
{
Text = "View Panel",
FontSize = 20,
FontWeight = FontWeights.Bold,
Margin = new Thickness(5),
HorizontalAlignment = HorizontalAlignment.Center
};
TextBlock largeFileHeading = new TextBlock
{
Text = "Search By File Size : ",
FontSize = 16,
FontWeight = FontWeights.Bold,
Margin = new Thickness(5),
};
TextBlock fileTypeHeading = new TextBlock
{
Text = "Search By File Type : ",
FontSize = 16,
FontWeight = FontWeights.Bold,
Margin = new Thickness(5),
};
TextBlock duplicateFileHeading = new TextBlock
{
Text = "Search By Duplicate File : ",
FontSize = 16,
FontWeight = FontWeights.Bold,
Margin = new Thickness(5),
};
// Create the Slider for number selection
numberSlider = new Slider
{
Minimum = 0,
Maximum = 1000000000,
TickFrequency = 1,
Margin = new Thickness(0, 5, 0, 0)
};
numberSlider.ValueChanged += NumberSlider_ValueChanged;
// Create the TextBlock to display the selected number from the slider
selectedNumberTextBlock = new TextBlock
{
Text = "Selected Size: 0",
FontSize = 14,
Margin = new Thickness(10),
HorizontalAlignment = HorizontalAlignment.Right
};
// Create the ListBox to display the paths of large-sized files
displayFiles = new ListBox
{
Width = double.NaN, // Set the width to double.NaN for auto-width
Height = 250,
MaxHeight = 300
};
// Create the TextBlock to display the number of large files
fileCountTextBlock = new TextBlock
{
Text = "File Count : 0",
Margin = new Thickness(5),
FontSize = 14,
};
// Create the Button to trigger finding large-sized files
findLargeSizedFilesButton = new Button
{
Content = "Search",
Width = 100,
Margin = new Thickness(5, 5, 10, 5),
Padding = new Thickness(10),
HorizontalAlignment = HorizontalAlignment.Left,
};
findLargeSizedFilesButton.Click += FindLargeSizedFiles_Click;
findLargeSizedFilesButton.Style = CreateRoundButtonStyle();
// Create the Delete Button
Button deleteButton = new Button
{
Content = "Delete",
Padding = new Thickness(10),
FontSize = 14,
Margin = new Thickness(0, 10, 0, 0)
};
deleteButton.Click += DeleteButton_Click;
deleteButton.Style = CreateRoundButtonStyle();
// Drop-down menu with values: (.txt, .docs, .pdf, .png, .jpg, .jpeg)
fileTypeComboBox = new ComboBox
{
Width = 100,
Margin = new Thickness(5),
Padding = new Thickness(10)
};
fileTypeComboBox.Items.Add(".txt");
fileTypeComboBox.Items.Add(".docx");
fileTypeComboBox.Items.Add(".pdf");
fileTypeComboBox.Items.Add(".png");
fileTypeComboBox.Items.Add(".jpg");
fileTypeComboBox.Items.Add(".jpeg");
// fileTypeComboBox.SelectedIndex = 0;
fileTypeComboBox.SelectionChanged += FileTypeComboBox_SelectionChanged;
Button searchButton = new Button
{
Content = "Search",
Margin = new Thickness(5),
Padding = new Thickness(10), // Add padding of 10 units
Width = 100
};
searchButton.Click += SearchButton_Click;
searchButton.Style = CreateRoundButtonStyle();
duplicateFileTypeComboBox = new ComboBox
{
Width = 100,
Margin = new Thickness(5, 5, 10, 5),
Padding = new Thickness(10)
};
duplicateFileTypeComboBox.SelectedIndex = 0;
duplicateFileTypeComboBox.Items.Add("All Files");
duplicateFileTypeComboBox.Items.Add("Image");
duplicateFileTypeComboBox.Items.Add("Video");
duplicateFileTypeComboBox.Items.Add("Document");
duplicateFileTypeComboBox.Items.Add("Audio");
// Search button with drop-down menu
Button duplicate_searchButton = new Button
{
Content = "Search",
Margin = new Thickness(5),
Padding = new Thickness(10), // Add padding of 10 units
Width = 100
};
duplicate_searchButton.Click += SearchButton_Click2;
duplicate_searchButton.Style = CreateRoundButtonStyle();
searchByFileTypePanel.Children.Add(fileTypeComboBox);
searchByFileTypePanel.Children.Add(searchButton);
duplicateFilePanel.Children.Add(duplicateFileTypeComboBox);
duplicateFilePanel.Children.Add(duplicate_searchButton);
// Add the TextBox, ComboBox, and Search Button to the searchPanel
StackPanel colouring = new StackPanel();
contentStackPanel.Children.Add(instructionTextBlock);
contentStackPanel.Children.Add(fileCountTextBlock);
colouring.Children.Add(displayFiles); // Add the ListBox to the content StackPanel
Border border = new Border
{
BorderThickness = new Thickness(3), // Thicker border (e.g., 3 pixels)
BorderBrush = Brushes.Black,
Margin = new Thickness(0.5), // Margin of 10 units on all sides
// Padding = new Thickness(10), // left top right bottom
Child = colouring // Set the StackPanel as the child of the Border
};
// Add all the elements to the content StackPanel
contentStackPanel.Children.Add(border); // Add the Delete Button to the content StackPanel
contentStackPanel.Children.Add(deleteButton); // Add the Delete Button to the content StackPanel
contentStackPanel.Children.Add(
new TextBlock
{
Text = " ",
FontSize = 16,
FontWeight = FontWeights.Bold,
Margin = new Thickness(5),
});
contentStackPanel.Children.Add(largeFileHeading);
contentStackPanel.Children.Add(numberSlider);
contentStackPanel.Children.Add(selectedNumberTextBlock);
contentStackPanel.Children.Add(findLargeSizedFilesButton);
contentStackPanel.Children.Add(
new TextBlock
{
Text = " ",
FontSize = 16,
FontWeight = FontWeights.Bold,
Margin = new Thickness(5),
});
// contentStackPanel.Children.Add(searchPanel); // Add the searchPanel to the content StackPanel
contentStackPanel.Children.Add(fileTypeHeading);
contentStackPanel.Children.Add(searchByFileTypePanel);
contentStackPanel.Children.Add(
new TextBlock
{
Text = " ",
FontSize = 16,
FontWeight = FontWeights.Bold,
Margin = new Thickness(5),
});
contentStackPanel.Children.Add(duplicateFileHeading);
contentStackPanel.Children.Add(duplicateFilePanel);
displayFiles.SelectionMode = SelectionMode.Extended; // Multiple selection delete
// Clear the previous content from the MAIN_AREA StackPanel
MAIN_AREA.Children.Clear();
// Add the content StackPanel to the MAIN_AREA StackPanel
MAIN_AREA.Children.Add(contentStackPanel);
}
private void updateCount()
{
fileCountTextBlock.Text = $"File Count : {displayFiles.Items.Count}";
}
// calculate duplicates from here
private void SearchButton_Click2(object sender, RoutedEventArgs e)
{
displayFiles.ItemsSource = null;
string selectedFileType = duplicateFileTypeComboBox.SelectedItem as string;
var folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();
if (folderBrowserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string searchDirectory = folderBrowserDialog.SelectedPath;
string searchPattern = GetSearchPattern(selectedFileType);
if (searchPattern == null)
{
System.Windows.MessageBox.Show("Invalid file type selected.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
string[] patterns = searchPattern.Split("|");
allFiles = null;
string tmp = searchDirectory;
foreach (var pattern in patterns)
{
// MessageBox.Show(pattern.TrimEnd().TrimStart());
if (allFiles == null) allFiles = Directory.GetFiles(searchDirectory, "*" + pattern, SearchOption.AllDirectories);
else allFiles = allFiles.Concat(Directory.GetFiles(searchDirectory, "*" + pattern, SearchOption.AllDirectories)).ToArray();
}
}
duplicateFilesMap.Clear();
// var progressDialog = new ProgressDialog();
// progressDialog.Show();
// Perform the search process asynchronously using Tasks
Task.Run(() =>
{
foreach (var file in allFiles)
{
string fileHash = CalculateFileHash(file);
lock (duplicateFilesMap)
{
if (!duplicateFilesMap.ContainsKey(fileHash))
{
duplicateFilesMap.Add(fileHash, new List<string>());
}
duplicateFilesMap[fileHash].Add(file);
}
}
// Filter out non-duplicate files
var duplicateFiles = duplicateFilesMap.Values.Where(d => d.Count > 1).ToList();
// Update the ListView with the duplicate files
foreach (var group in duplicateFiles)
{
lock (foundFiles)
{
foreach (var file in group)
{
foundFiles.Add(file);
}
}
}
// Update the UI once the search process is complete
System.Windows.Application.Current.Dispatcher.Invoke(() =>
{
displayFiles.ItemsSource = foundFiles;
updateCount();
if (duplicateFiles.Count == 0)
{
displayFiles.ItemsSource = null;
updateCount();
System.Windows.MessageBox.Show("No duplicate files found.", "Result", MessageBoxButton.OK, MessageBoxImage.Information);
}
});
});
// progressDialog.Close();
}
private static string GetSearchPattern(string fileType)
{
string tmp = "";
if (fileType == "Image") tmp = ".jpg|.jpeg|.png|.gif|.bmp|.tiff|.tif|.webp|.svg";
else if (fileType == "Video") tmp = ".mp4|.avi|.mkv|.mov|.wmv|.flv|.webm|.m4v|.mpeg|.mpg|.3gp|.3g2";
else if (fileType == "Document") tmp = ".doc|.docx|.pdf|.rtf|.ppt|.pptx|.txt|.xls|.xlsx|.odt|.ods|.odp|.csv";
else if (fileType == "Audio") tmp = ".mp3|.wav|.m4a|.flac|.ogg|.aac|.wma|.aiff|.alac|.amr|.opus";
return tmp;
}
private string CalculateFileHash(string filePath)
{
using (var md5 = MD5.Create())
{
using (var stream = System.IO.File.OpenRead(filePath))
{
byte[] hash = md5.ComputeHash(stream);
return BitConverter.ToString(hash).Replace("-", "").ToLower();
}
}
}
// duplicates file end
private void SearchButton_Click(object sender, RoutedEventArgs e) // SPECIFIC FORMAT SEARCH
{
displayFiles.ItemsSource = null;
// Show a folder browser dialog to select the search directory
foundFiles.Clear();
var folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();
if (folderBrowserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string searchDirectory = folderBrowserDialog.SelectedPath;
// Search for files with the specified format in the chosen directory
foundFiles = Directory.GetFiles(searchDirectory, "*" + fileFormat, SearchOption.AllDirectories).ToList();
// Display the found files in the list view
displayFiles.ItemsSource = null; // Clear the ListBox's ItemsSource before updating
displayFiles.ItemsSource = foundFiles;
updateCount();
}
if (foundFiles.Count == 0)
{
System.Windows.MessageBox.Show("No file found.", "Result", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
private void FileTypeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (fileTypeComboBox.SelectedItem != null)
{
// Get the selected item from the ComboBox
fileFormat = fileTypeComboBox.SelectedItem as string;
// Your code to handle the selection change goes here
// For example, you can display a message with the selected file type:
MessageBox.Show("Selected File Type: " + fileFormat);
}
else
{
// Handle the case when no item is selected
MessageBox.Show("No File Type selected.");
}
}
// SPECIFIC FORMAT SEARCH END
// DELETE BUTTON
private void DeleteButton_Click(object sender, RoutedEventArgs e)
{
if (displayFiles.SelectedItems.Count > 0)
{
try
{
// Create a copy of the selected items since we'll be modifying the collection
//var selectedItems = displayFiles.SelectedItems.Cast<string>().ToList();
// Loop through the selected items and delete each file
foreach (var selectedItem in displayFiles.SelectedItems.OfType<string>().ToList())
{
System.IO.File.Delete(selectedItem);
foundFiles.Remove(selectedItem);
}
// Refresh the list view and update the count
displayFiles.Items.Refresh();
updateCount();
}
catch (Exception ex)
{
// Handle any exception that occurred while deleting the files
System.Windows.MessageBox.Show($"Error deleting files: {ex.Message}");
}
}
else
{
System.Windows.MessageBox.Show("Please select files to delete.");
}
}
// SLIDER
private Tuple<double, string, double> give_slider_value(double total_value, double MAXVAL)
{
double total_size = total_value, actual_size = 0, difference = 0.3 * MAXVAL;
string type = "Byte";
if (total_size > 0.7 * MAXVAL) // GB
{
total_size = 1 + 9 * (total_size - 0.7 * MAXVAL) / difference;
total_size = Math.Round(total_size, 2);
actual_size = total_size * 1e9;
type = "GB";
}
else if (total_size > 0.4 * MAXVAL) // MB
{
total_size = 1 + 1e3 * (total_size - 0.4 * MAXVAL) / difference;
total_size = Math.Round(total_size, 1);
actual_size = total_size * 1e6;
type = "MB";
}
else if (total_size > 0.1 * MAXVAL) // KB
{
total_size = 1 + 1e3 * (total_size - 0.1 * MAXVAL) / difference;
total_size = Math.Round(total_size, 1);
actual_size = total_size * 1e3;
type = "KB";
}
else
{
difference = 0.1 * MAXVAL;
total_size = total_size * 1e3 / difference;
actual_size = total_size;
total_size = Math.Round(total_size);
}
return Tuple.Create(total_size, type, actual_size); // slider size , type , file size to search
}
private void NumberSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
var res = give_slider_value(numberSlider.Value, numberSlider.Maximum);
selectedNumberTextBlock.Text = $"Selected Number: {res.Item1} {res.Item2}";
}
// --------------------------- Find Function ----------------------------------
private void FindLargeSizedFiles_Click(object sender, RoutedEventArgs e)
{
try
{
count = 0;
var res = give_slider_value(numberSlider.Value, numberSlider.Maximum);
minSize = (long)res.Item3;
var folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();
var directoriesToProcess = new Queue<string>();
if (folderBrowserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string rootDirectory = folderBrowserDialog.SelectedPath;
directoriesToProcess.Enqueue(rootDirectory);
while (directoriesToProcess.Count > 0)
{
string currentDir = directoriesToProcess.Dequeue();
CountLargeSizedFiles(currentDir);
// Update the ListBox to show the directories with large-sized files
Helper(currentDir);
updateCount();
}
if (count > 200)
{
System.Windows.MessageBox.Show("More than 200 directories contain large-sized files. Showing only the top 200 directories.", "Result Limit Reached", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
}
catch (Exception ex)
{
MessageBox.Show($"Error: {ex.Message}");
}
}
private void Helper(string rootDirectory)
{
foundFiles.Clear();
var directoriesToCheck = new Stack<string>();
directoriesToCheck.Push(rootDirectory);
//long limit = (long)1e9; // Set a limit for the number of directories to traverse
int directoryCount = 0;
int fileLimit = 200; // Set a limit for the number of files to display
// long maxSize = 1024 * 1024 * 1024; // Set a limit for the file size (e.g., 1 GB)
while (directoriesToCheck.Count > 0) // CHANGE