-
Notifications
You must be signed in to change notification settings - Fork 0
/
StorageManagerPage.xaml.cs
151 lines (139 loc) · 5.67 KB
/
StorageManagerPage.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using Windows.Storage;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace SimpleWindowsTool
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class StorageManagerPage : Page
{
//variables
List<string> driveLetters = new List<string>();
bool clearingLogs = false;
public StorageManagerPage()
{
this.InitializeComponent();
GetDriveLetter();
InitializeUI();
}
private void GetDriveLetter()
{
//https://learn.microsoft.com/en-us/dotnet/api/system.io.driveinfo.getdrives?view=net-8.0
DriveInfo[] allDrives = DriveInfo.GetDrives();
driveLetters.Clear();
foreach (DriveInfo drive in allDrives)
{
driveLetters.Add(drive.Name);
}
}
private void InitializeUI()
{
ChooseDiskComboBox.Items.Clear();
foreach (string drive in driveLetters)
{
ChooseDiskComboBox.Items.Add(drive);
}
try
{
ChooseDiskComboBox.SelectedIndex = 0;
}
catch { }
}
private async void ClearLogsButton_Click(object sender, RoutedEventArgs e)
{
if (!clearingLogs)
{
clearingLogs = true;
ClearLogsButton.Content = "Cancel";
ChooseDiskComboBox.IsEnabled = false;
ClearLogsProgressRing.IsActive = true;
ClearLogsFinishedTick.Visibility = Visibility.Collapsed;
string directoryPath = ChooseDiskComboBox.SelectedItem.ToString();
if(File.Exists("E:\\New Text Document.log")){
StorageFile storageFile = await StorageFile.GetFileFromPathAsync("E:\\New Text Document.log");
await storageFile.DeleteAsync(StorageDeleteOption.PermanentDelete);
}
try
{
StorageFolder storageFolder = await StorageFolder.GetFolderFromPathAsync(directoryPath);
IReadOnlyList<StorageFolder> folderList = await storageFolder.GetFoldersAsync();
IReadOnlyList<StorageFile> storageFiles = await storageFolder.GetFilesAsync();
foreach (StorageFile storageFile in storageFiles)
{
if (Path.GetExtension(storageFile.Path).ToLower() == ".log")
{
if (clearingLogs)
{
try
{
await storageFile.DeleteAsync(StorageDeleteOption.PermanentDelete);
}
catch { }
}
}
}
foreach (StorageFolder folder in folderList)
{
string[] fileNames = Directory.GetFiles(folder.Path, "*", SearchOption.AllDirectories);
foreach (string file in fileNames)
{
if (File.Exists(file))
{
if (Path.GetExtension(file).ToLower() == ".log")
{
StorageFile targetFile = await StorageFile.GetFileFromPathAsync(file);
if (clearingLogs)
{
try
{
await targetFile.DeleteAsync(StorageDeleteOption.PermanentDelete);
}
catch { }
}
else
{
break;
}
}
}
}
}
}
catch
{
}
clearingLogs = false;
ClearLogsButton.Content = "Clear Logs";
ChooseDiskComboBox.IsEnabled = true;
ClearLogsProgressRing.IsActive = false;
ClearLogsFinishedTick.Visibility = Visibility.Visible;
}
else
{
clearingLogs = false;
ClearLogsButton.Content = "Clear Logs";
ChooseDiskComboBox.IsEnabled = true;
ClearLogsProgressRing.IsActive = false;
}
}
private void ChooseDiskComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ClearLogsFinishedTick.Visibility = Visibility.Collapsed;
}
}
}