Portable Tool for an Ideal Windows Setup
C# activatorbloatware-removalcsharpdebloatdebloaterdisable-servicesmonitoring-systemportableprivacy-toolsregistry-tweakssystem-tuningsystem-utilitytelemetrytweakerwindows-10windows-11windows-debloatwindows-defenderwindows-optimizationwindows-tweaks
add WMI event to monitor free and used disk space
Greedeks39fc538c06cc
diff --git a/.Source/GTweak/Languages/ru/Localize.xaml b/.Source/GTweak/Languages/ru/Localize.xamlindex 75c73536..49dadc1b 100644--- a/.Source/GTweak/Languages/ru/Localize.xaml+++ b/.Source/GTweak/Languages/ru/Localize.xaml@@ -522,7 +522,7 @@<v:String x:Key="title_ram_sysinfo">Оперативная память:</v:String><v:String x:Key="title_mtype_sysinfo">Тип памяти:</v:String><v:String x:Key="title_storage_sysinfo">Устройства хранения данных:</v:String>- <v:String x:Key="title_storage_used_sysinfo">Использовано:</v:String>+ <v:String x:Key="title_storage_used_sysinfo">Занято:</v:String><v:String x:Key="title_storage_free_sysinfo">Свободно:</v:String><v:String x:Key="title_sound_sysinfo">Устройства воспроизведения:</v:String><v:String x:Key="title_netadapter_sysinfo">Сетевые адаптеры:</v:String>diff --git a/.Source/GTweak/Languages/uk/Localize.xaml b/.Source/GTweak/Languages/uk/Localize.xamlindex 82b0ba79..97200a75 100644--- a/.Source/GTweak/Languages/uk/Localize.xaml+++ b/.Source/GTweak/Languages/uk/Localize.xaml@@ -522,7 +522,7 @@<v:String x:Key="title_ram_sysinfo">Оперативна пам’ять:</v:String><v:String x:Key="title_mtype_sysinfo">Тип пам'яті:</v:String><v:String x:Key="title_storage_sysinfo">Пристрої зберігання даних:</v:String>- <v:String x:Key="title_storage_used_sysinfo">Використано:</v:String>+ <v:String x:Key="title_storage_used_sysinfo">Зайнято:</v:String><v:String x:Key="title_storage_free_sysinfo">Вільно:</v:String><v:String x:Key="title_sound_sysinfo">Пристрої відтворення:</v:String><v:String x:Key="title_netadapter_sysinfo">Мережеві адаптери:</v:String>diff --git a/.Source/GTweak/Utilities/Configuration/HardwareProvider.cs b/.Source/GTweak/Utilities/Configuration/HardwareProvider.csindex 5d2a8dce..9caa96f1 100644--- a/.Source/GTweak/Utilities/Configuration/HardwareProvider.cs+++ b/.Source/GTweak/Utilities/Configuration/HardwareProvider.cs@@ -99,22 +99,24 @@ namespace GTweak.Utilities.Configuration{if (deviceType == DeviceType.Storage || deviceType == DeviceType.All){- var (Data, FreePercentage, UsedPercentage) = GetStorageDevices();- Storage.Data = Data;+ Storage.Data = GetStorageDevices();+ }++ if (deviceType == DeviceType.StorageSpace || deviceType == DeviceType.All || deviceType == DeviceType.Storage)+ {+ (string FreePercentage, string UsedPercentage) = GetStorageSpace();Storage.FreeSpace = FreePercentage;Storage.UsedSpace = UsedPercentage;}if (deviceType == DeviceType.Audio || deviceType == DeviceType.All){- string value = GetAudioDevices();- AudioDevice = value;+ AudioDevice = GetAudioDevices();}if (deviceType == DeviceType.Network || deviceType == DeviceType.All){- string value = GetNetworkAdapters();- NetworkAdapter = value;+ NetworkAdapter = GetNetworkAdapters();}}@@ -397,29 +399,10 @@ namespace GTweak.Utilities.Configuration/// <summary>/// The MSFT_PhysicalDisk class may be missing or malfunctioning; in such cases, it will be replaced by the universal Win32_DiskDrive class./// </summary>- private (string Data, string FreePercentage, string UsedPercentage) GetStorageDevices()+ private string GetStorageDevices(){StringBuilder result = new StringBuilder();- long totalSize = 0;- long totalFreeSpace = 0;-- foreach (var drive in DriveInfo.GetDrives())- {- try- {- if (drive.IsReady && (drive.DriveType == DriveType.Fixed || drive.DriveType == DriveType.Removable))- {- totalSize += drive.TotalSize;- totalFreeSpace += drive.TotalFreeSpace;- }- }- catch (Exception ex) { ErrorLogging.LogDebug(ex); }- }-- string freePercent = totalSize > 0 ? $"{(Math.Round((double)totalFreeSpace / totalSize * 100, 1)).ToString("0.#", CultureInfo.InvariantCulture)}%" : string.Empty;- string usedPercent = totalSize > 0 ? $"{(Math.Round(100 - ((double)totalFreeSpace / totalSize * 100), 1)).ToString("0.#", CultureInfo.InvariantCulture)}%" : string.Empty;-static string GetStorageType(object mediaType, string deviceId, ushort busType, string interfaceType){string storageType = HardwareMappings.MediaTypeMap.FirstOrDefault(x => x.Keys != null && x.Keys.Any(k => k is string ks && mediaType is string ms ? string.Equals(ks, ms, StringComparison.OrdinalIgnoreCase) : Equals(k, mediaType))).Type ?? StorageTypeLabels.Unspecified;@@ -486,7 +469,31 @@ namespace GTweak.Utilities.Configuration}}- return (result.ToString().TrimEnd('\n', '\r'), freePercent, usedPercent);+ return result.ToString().TrimEnd('\n', '\r');+ }++ private (string FreePercentage, string UsedPercentage) GetStorageSpace()+ {+ long totalSize = 0;+ long totalFreeSpace = 0;++ foreach (DriveInfo drive in DriveInfo.GetDrives())+ {+ try+ {+ if (drive.IsReady && (drive.DriveType == DriveType.Fixed || drive.DriveType == DriveType.Removable))+ {+ totalSize += drive.TotalSize;+ totalFreeSpace += drive.TotalFreeSpace;+ }+ }+ catch (Exception ex) { ErrorLogging.LogDebug(ex); }+ }++ string freePercent = totalSize > 0 ? $"{(Math.Round((double)totalFreeSpace / totalSize * 100, 1)).ToString("0.#", CultureInfo.InvariantCulture)}%" : string.Empty;+ string usedPercent = totalSize > 0 ? $"{(Math.Round(100 - ((double)totalFreeSpace / totalSize * 100), 1)).ToString("0.#", CultureInfo.InvariantCulture)}%" : string.Empty;++ return (freePercent, usedPercent);}/// <summary>diff --git a/.Source/GTweak/Utilities/Configuration/MonitoringProvider.cs b/.Source/GTweak/Utilities/Configuration/MonitoringProvider.csindex d9b70bb2..47d5de1f 100644--- a/.Source/GTweak/Utilities/Configuration/MonitoringProvider.cs+++ b/.Source/GTweak/Utilities/Configuration/MonitoringProvider.cs@@ -18,7 +18,7 @@ namespace GTweak.Utilities.Configurationprivate readonly List<(ManagementEventWatcher watcher, EventArrivedEventHandler handler)> _watcherHandler = new List<(ManagementEventWatcher watcher, EventArrivedEventHandler handler)>();private readonly ServiceController[] _servicesList = ServiceController.GetServices();- internal enum DeviceType { All, Storage, Audio, Network }+ internal enum DeviceType { All, Storage, StorageSpace, Audio, Network }[StructLayout(LayoutKind.Sequential)]private struct DEVMODE@@ -225,25 +225,27 @@ namespace GTweak.Utilities.Configurationinternal async Task StartDeviceMonitoring(){- await Task.WhenAll(new List<(string filter, DeviceType type, string scope)>+ await Task.WhenAll(new List<(string eventClass, TimeSpan within, string condition, DeviceType type, string scope)>{- ($"TargetInstance ISA {(HardwareProvider.isMsftAvailable ? "'MSFT_PhysicalDisk'" : "'Win32_DiskDrive'")}", DeviceType.Storage, HardwareProvider.isMsftAvailable ? @"root\microsoft\windows\storage" : null),- ("TargetInstance ISA 'Win32_SoundDevice'", DeviceType.Audio, null),- ("TargetInstance ISA 'Win32_NetworkAdapter' AND TargetInstance.NetConnectionStatus IS NOT NULL", DeviceType.Network, null)- }.Select(device => Task.Run(() => SubscribeToDeviceEvents(device.filter, device.type, device.scope))).ToArray());+ ("__InstanceOperationEvent", TimeSpan.FromSeconds(2), $"TargetInstance ISA {(HardwareProvider.isMsftAvailable ? "'MSFT_PhysicalDisk'" : "'Win32_DiskDrive'")}", DeviceType.Storage, HardwareProvider.isMsftAvailable ? @"root\microsoft\windows\storage" : null),+ ("__InstanceOperationEvent", TimeSpan.FromSeconds(2), "TargetInstance ISA 'Win32_SoundDevice'", DeviceType.Audio, null),+ ("__InstanceOperationEvent", TimeSpan.FromSeconds(2), "TargetInstance ISA 'Win32_NetworkAdapter' AND TargetInstance.NetConnectionStatus IS NOT NULL", DeviceType.Network, null),+ ("__InstanceModificationEvent", TimeSpan.FromSeconds(5), "TargetInstance ISA 'Win32_LogicalDisk'", DeviceType.StorageSpace, null)++ }.Select(device => Task.Run(() => SubscribeToDeviceEvents(device.eventClass, device.within, device.condition, device.type, device.scope))).ToArray());SystemEvents.DisplaySettingsChanged += OnDisplaySettingsChanged;}private void OnDisplaySettingsChanged(object sender, EventArgs e) => GetPrimaryRefreshRate();- private async Task SubscribeToDeviceEvents(string filter, DeviceType type, string scope)+ private async Task SubscribeToDeviceEvents(string eventClassName, TimeSpan within, string condition, DeviceType type, string scope){try{await Task.Run(() =>{- WqlEventQuery query = new WqlEventQuery("__InstanceOperationEvent", TimeSpan.FromSeconds(2), filter);+ WqlEventQuery query = new WqlEventQuery(eventClassName, within, condition);ManagementEventWatcher managementEvent = new ManagementEventWatcher(new ManagementScope(scope ?? @"root\cimv2"), query);void handler(object s, EventArrivedEventArgs e){@@ -265,12 +267,12 @@ namespace GTweak.Utilities.Configurationtry{watcher.EventArrived -= handler;- watcher.Stop();- watcher.Dispose();+ watcher?.Stop();+ watcher?.Dispose();}catch (Exception ex) { ErrorLogging.LogDebug(ex); }}- _watcherHandler.Clear();+ _watcherHandler?.Clear();SystemEvents.DisplaySettingsChanged -= OnDisplaySettingsChanged;}}diff --git a/.Source/GTweak/View/DataSystemView.xaml b/.Source/GTweak/View/DataSystemView.xamlindex a67e13e6..08ac875d 100644--- a/.Source/GTweak/View/DataSystemView.xaml+++ b/.Source/GTweak/View/DataSystemView.xaml@@ -171,7 +171,7 @@</Border><TextBlock Style="{StaticResource TextDefault}" HorizontalAlignment="Left" Margin="5,1,0,0" Foreground="{StaticResource Brush_Highlighted}" Height="Auto" Width="Auto"><Run Text="{Binding [Bios].Data}" PreviewMouseDown="HandleCopyingData_PreviewMouseDown" Cursor="Hand"/><LineBreak/>- <Run Text="{DynamicResource title_mode_sysinfo}" Foreground="{StaticResource Brush_Accent}"/>+ <Run Text="{DynamicResource title_mode_sysinfo}" Foreground="{StaticResource Brush_Toggle_Text}"/><Run Text="{Binding [Mode].Data}" PreviewMouseDown="HandleCopyingData_PreviewMouseDown" Cursor="Hand"/></TextBlock></StackPanel>@@ -193,7 +193,7 @@</Border><TextBlock Style="{StaticResource TextDefault}" HorizontalAlignment="Left" Margin="5,1,0,0" Foreground="{StaticResource Brush_Highlighted}" Height="Auto" Width="Auto"><Run Text="{Binding [Motherboard].Data}" PreviewMouseDown="HandleCopyingData_PreviewMouseDown" Cursor="Hand"/><LineBreak/>- <Run Text="{DynamicResource title_chipset_sysinfo}" Foreground="{StaticResource Brush_Accent}"/>+ <Run Text="{DynamicResource title_chipset_sysinfo}" Foreground="{StaticResource Brush_Toggle_Text}"/><Run Text="{Binding [Chipset].Data}" PreviewMouseDown="HandleCopyingData_PreviewMouseDown" Cursor="Hand"/></TextBlock></StackPanel>@@ -215,10 +215,10 @@</Border><TextBlock Style="{StaticResource TextDefault}" HorizontalAlignment="Left" Margin="5,1,0,0" Foreground="{StaticResource Brush_Highlighted}" Height="Auto" Width="Auto"><Run Text="{Binding [Processor].Data}" PreviewMouseDown="HandleCopyingData_PreviewMouseDown" Cursor="Hand"/><LineBreak/>- <Run Text="{DynamicResource title_cores_sysinfo}" Foreground="{StaticResource Brush_Accent}"/>+ <Run Text="{DynamicResource title_cores_sysinfo}" Foreground="{StaticResource Brush_Toggle_Text}"/><Run Text="{Binding [Cores].Data}"/><Run Text=" | " FontWeight="Bold" Foreground="{StaticResource Brush_Inactive}"/>- <Run Text="{DynamicResource title_threads_sysinfo}" Foreground="{StaticResource Brush_Accent}"/>+ <Run Text="{DynamicResource title_threads_sysinfo}" Foreground="{StaticResource Brush_Toggle_Text}"/><Run Text="{Binding [Threads].Data}"/></TextBlock></StackPanel>@@ -240,7 +240,7 @@</Border><TextBlock Style="{StaticResource TextDefault}" HorizontalAlignment="Left" Margin="5,1,0,0" Foreground="{StaticResource Brush_Highlighted}" Height="Auto" Width="Auto"><Run Text="{Binding [Graphics].Data}" PreviewMouseDown="HandleCopyingData_PreviewMouseDown" Cursor="Hand"/><LineBreak/>- <Run Text="{DynamicResource title_refreshrate_sysinfo}" Foreground="{StaticResource Brush_Accent}"/>+ <Run Text="{DynamicResource title_refreshrate_sysinfo}" Foreground="{StaticResource Brush_Toggle_Text}"/><Run Text="{Binding [RefreshRate].Data}"/></TextBlock></StackPanel>@@ -262,7 +262,7 @@</Border><TextBlock Style="{StaticResource TextDefault}" HorizontalAlignment="Left" Margin="5,1,0,0" Foreground="{StaticResource Brush_Highlighted}" Height="Auto" Width="Auto"><Run Text="{Binding [Memory].Data}" PreviewMouseDown="HandleCopyingData_PreviewMouseDown" Cursor="Hand"/><LineBreak/>- <Run Text="{DynamicResource title_mtype_sysinfo}" Foreground="{StaticResource Brush_Accent}"/>+ <Run Text="{DynamicResource title_mtype_sysinfo}" Foreground="{StaticResource Brush_Toggle_Text}"/><Run Text="{Binding [Type].Data}" PreviewMouseDown="HandleCopyingData_PreviewMouseDown" Cursor="Hand"/></TextBlock></StackPanel>@@ -288,13 +288,12 @@</Border><TextBlock Style="{StaticResource TextDefault}" HorizontalAlignment="Left" Margin="5,1,0,0" Foreground="{StaticResource Brush_Highlighted}" Height="Auto" Width="Auto"><Run Text="{Binding [Storage].Data}" PreviewMouseDown="HandleCopyingData_PreviewMouseDown" Cursor="Hand"/><LineBreak/>- <Run Text="{DynamicResource title_storage_used_sysinfo}" Foreground="{StaticResource Brush_Accent}"/>- <Run Text="{Binding [UsedSpace].Data}"/>- <Run Text=" | " FontWeight="Bold" Foreground="{StaticResource Brush_Inactive}"/>- <Run Text="{DynamicResource title_storage_free_sysinfo}" Foreground="{StaticResource Brush_Accent}"/>+ <Run Text="{DynamicResource title_storage_free_sysinfo}" Foreground="{StaticResource Brush_Toggle_Text}"/><Run Text="{Binding [FreeSpace].Data}"/>+ <Run Text=" | " FontWeight="Bold" Foreground="{StaticResource Brush_Inactive}"/>+ <Run Text="{DynamicResource title_storage_used_sysinfo}" Foreground="{StaticResource Brush_Toggle_Text}"/>+ <Run Text="{Binding [UsedSpace].Data}"/></TextBlock>-</StackPanel><StackPanel Orientation="Vertical" Margin="0,20,0,0">