-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
233 lines (204 loc) · 7.86 KB
/
MainWindow.xaml.cs
File metadata and controls
233 lines (204 loc) · 7.86 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
using Microsoft.VisualBasic.FileIO;
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace GarbageCollector
{
public partial class MainWindow : Window
{
private readonly string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
public ObservableCollection<FileItem> Files { get; set; } = new ObservableCollection<FileItem>();
public MainWindow()
{
InitializeComponent();
LoadFiles();
}
private void LoadFiles()
{
Files.Clear();
// Add folders
foreach (var dir in Directory.GetDirectories(desktopPath))
{
Files.Add(new FileItem
{
Name = Path.GetFileName(dir),
FullPath = dir,
Icon = IconHelper.GetIcon(dir, true),
IsFolder = true
});
}
// Add files
foreach (var file in Directory.GetFiles(desktopPath))
{
Files.Add(new FileItem
{
Name = Path.GetFileName(file),
FullPath = file,
Icon = IconHelper.GetIcon(file, false),
IsFolder = false
});
}
FileList.ItemsSource = Files;
}
private void DeleteSelected_Click(object sender, RoutedEventArgs e)
{
var selected = Files.Where(f => f.IsChecked).ToList();
if (!selected.Any())
{
MessageBox.Show("No items selected.", "GarbageCollector", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
// CONFIRMATION
var result = MessageBox.Show(
$"Are you sure you want to delete {selected.Count} selected item(s)? They will be sent to the Recycle Bin.",
"Confirm Delete",
MessageBoxButton.YesNo,
MessageBoxImage.Warning);
if (result != MessageBoxResult.Yes) return;
foreach (var f in selected)
{
try
{
if (f.IsFolder)
{
Microsoft.VisualBasic.FileIO.FileSystem.DeleteDirectory(f.FullPath,
Microsoft.VisualBasic.FileIO.UIOption.OnlyErrorDialogs,
Microsoft.VisualBasic.FileIO.RecycleOption.SendToRecycleBin);
}
else
{
Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile(f.FullPath,
Microsoft.VisualBasic.FileIO.UIOption.OnlyErrorDialogs,
Microsoft.VisualBasic.FileIO.RecycleOption.SendToRecycleBin);
}
}
catch (Exception ex)
{
MessageBox.Show($"Error deleting {f.Name}: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
LoadFiles();
}
private void OrganizeByType_Click(object sender, RoutedEventArgs e)
{
// CONFIRMATION
var result = MessageBox.Show(
"Are you sure you want to organize your desktop files into folders by type?",
"Confirm Organize",
MessageBoxButton.YesNo,
MessageBoxImage.Question);
if (result != MessageBoxResult.Yes) return;
foreach (var file in Directory.GetFiles(desktopPath))
{
string ext = Path.GetExtension(file).Trim('.').ToUpper();
if (string.IsNullOrEmpty(ext)) ext = "OTHERS";
string folder = Path.Combine(desktopPath, ext);
Directory.CreateDirectory(folder);
string destPath = Path.Combine(folder, Path.GetFileName(file));
try
{
if (!File.Exists(destPath))
File.Move(file, destPath);
}
catch (Exception ex)
{
MessageBox.Show($"Error moving {file}: {ex.Message}");
}
}
LoadFiles();
}
private void Refresh_Click(object sender, RoutedEventArgs e) => LoadFiles();
private void FileList_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (FileList.SelectedItem is FileItem item)
{
try
{
var psi = new ProcessStartInfo
{
FileName = item.FullPath,
UseShellExecute = true
};
Process.Start(psi);
}
catch (Exception ex)
{
MessageBox.Show($"Cannot open {item.Name}: {ex.Message}");
}
}
}
private void About_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(
"GarbageCollector\n\nA simple desktop organizer.\n\nCreated by mikeph_ 2025\nhttps://github.com/mikeph52\n\nVersion Alpha Testing",
"About GarbageCollector",
MessageBoxButton.OK,
MessageBoxImage.Information);
}
}
// ========== FileItem ==========
public class FileItem
{
public string Name { get; set; }
public string FullPath { get; set; }
public bool IsFolder { get; set; }
public bool IsChecked { get; set; }
public ImageSource Icon { get; set; }
}
// ========== IconHelper ==========
internal static class IconHelper
{
private const uint SHGFI_ICON = 0x000000100;
private const uint SHGFI_SMALLICON = 0x000000001;
private const uint FILE_ATTRIBUTE_DIRECTORY = 0x10;
private const uint FILE_ATTRIBUTE_NORMAL = 0x80;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
private struct SHFILEINFO
{
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
}
[DllImport("shell32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbFileInfo, uint uFlags);
[DllImport("user32.dll", SetLastError = true)]
private static extern bool DestroyIcon(IntPtr hIcon);
public static ImageSource GetIcon(string path, bool isFolder)
{
try
{
SHFILEINFO shinfo = new SHFILEINFO();
uint flags = SHGFI_ICON | SHGFI_SMALLICON;
uint attrs = isFolder ? FILE_ATTRIBUTE_DIRECTORY : FILE_ATTRIBUTE_NORMAL;
SHGetFileInfo(path, attrs, ref shinfo, (uint)Marshal.SizeOf(shinfo), flags);
if (shinfo.hIcon != IntPtr.Zero)
{
var img = Imaging.CreateBitmapSourceFromHIcon(
shinfo.hIcon,
Int32Rect.Empty,
BitmapSizeOptions.FromWidthAndHeight(16, 16));
img.Freeze();
DestroyIcon(shinfo.hIcon);
return img;
}
}
catch
{
// fallback: no icon
}
return null;
}
}
}