diff --git a/MystatDesktopWpf/Services/MystatAPICachingService.cs b/MystatDesktopWpf/Services/MystatAPICachingService.cs index 74898de..adf27b7 100644 --- a/MystatDesktopWpf/Services/MystatAPICachingService.cs +++ b/MystatDesktopWpf/Services/MystatAPICachingService.cs @@ -10,7 +10,7 @@ namespace MystatDesktopWpf.Services { - internal static class MystatAPICachingService + internal static class MystatAPICachingService { private static readonly MystatAPIClient api; private static string? userCachePath; @@ -111,28 +111,32 @@ public async static Task ClearCacheAsync() }); } - public async static Task GetCacheSize() + public async static Task GetCacheSize() { return await Task.Run(() => { - int size = 0; + long sizeInBytes = 0; + try { - foreach (var dir in Directory.GetDirectories(rootCachePath)) + string[] fileNames = Directory.GetFiles(rootCachePath, "*.*", SearchOption.AllDirectories); + + foreach (string name in fileNames) { - foreach (var file in Directory.GetFiles(dir)) - { - size += file.Length; - } + FileInfo info = new FileInfo(name); + sizeInBytes += info.Length; } + + return sizeInBytes; } - catch (Exception) {} - return size; + catch (Exception) { } + + return sizeInBytes; }); } - private static void CreateUserCacheDir() + private static void CreateUserCacheDir() { Directory.CreateDirectory(userCachePath); } diff --git a/MystatDesktopWpf/UserControls/SettingsSections/CacheSettings.xaml.cs b/MystatDesktopWpf/UserControls/SettingsSections/CacheSettings.xaml.cs index ce7ad05..17b9bbd 100644 --- a/MystatDesktopWpf/UserControls/SettingsSections/CacheSettings.xaml.cs +++ b/MystatDesktopWpf/UserControls/SettingsSections/CacheSettings.xaml.cs @@ -21,6 +21,8 @@ namespace MystatDesktopWpf.UserControls.SettingsSections /// public partial class CacheSettings : UserControl { + static string[] cacheSizeLabels = new string[] { "B", "KB", "MB", "GB" }; + public CacheSettings() { InitializeComponent(); @@ -48,7 +50,21 @@ private async void ClearCacheButton_Click(object sender, RoutedEventArgs e) public async void UpdateCacheSize() { CacheSizeTextBlock.SetResourceReference(TextBlock.TextProperty, "m_Calculating"); - CacheSizeTextBlock.Text = Math.Round(await MystatAPICachingService.GetCacheSize() / 1024.0, 2) + " KB"; + + long cacheSizeInBytes = await MystatAPICachingService.GetCacheSize(); + + + double cacheSize = cacheSizeInBytes; + string label = cacheSizeLabels[0]; + + int i = 0; + while (cacheSize >= 1024) + { + cacheSize = Math.Round(cacheSize / 1024.0, 2, MidpointRounding.ToZero); + label = cacheSizeLabels[++i]; + } + + CacheSizeTextBlock.Text = $"{cacheSize} {label}"; } } }