Skip to content

Commit 9030bff

Browse files
authored
Merge pull request #3992 from Flow-Launcher/clear_cache_twice
Fix DirectoryNotFoundException when deleting cache twice
2 parents 820304c + 4bea410 commit 9030bff

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

Flow.Launcher/SettingPages/ViewModels/SettingsPaneAboutViewModel.cs

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -231,35 +231,41 @@ private bool ClearCacheFolder()
231231
}
232232
});
233233

234-
// Firstly, delete plugin cache directories
235-
pluginCacheDirectory.EnumerateDirectories("*", SearchOption.TopDirectoryOnly)
236-
.ToList()
237-
.ForEach(dir =>
238-
{
239-
try
240-
{
241-
// Plugin may create directories in its cache directory
242-
dir.Delete(recursive: true);
243-
}
244-
catch (Exception e)
245-
{
246-
App.API.LogException(ClassName, $"Failed to delete cache directory: {dir.Name}", e);
247-
success = false;
248-
}
249-
});
250-
251-
// Then, delete plugin directory
252-
var dir = GetPluginCacheDir();
253-
try
254-
{
255-
dir.Delete(recursive: false);
256-
}
257-
catch (Exception e)
234+
// Check if plugin cache directory exists before attempting to delete
235+
// Or it will throw DirectoryNotFoundException in `pluginCacheDirectory.EnumerateDirectories`
236+
if (pluginCacheDirectory.Exists)
258237
{
259-
App.API.LogException(ClassName, $"Failed to delete cache directory: {dir.Name}", e);
260-
success = false;
238+
// Firstly, delete plugin cache directories
239+
pluginCacheDirectory.EnumerateDirectories("*", SearchOption.TopDirectoryOnly)
240+
.ToList()
241+
.ForEach(dir =>
242+
{
243+
try
244+
{
245+
// Plugin may create directories in its cache directory
246+
dir.Delete(recursive: true);
247+
}
248+
catch (Exception e)
249+
{
250+
App.API.LogException(ClassName, $"Failed to delete cache directory: {dir.Name}", e);
251+
success = false;
252+
}
253+
});
254+
255+
// Then, delete plugin directory
256+
var dir = pluginCacheDirectory;
257+
try
258+
{
259+
dir.Delete(recursive: false);
260+
}
261+
catch (Exception e)
262+
{
263+
App.API.LogException(ClassName, $"Failed to delete cache directory: {dir.Name}", e);
264+
success = false;
265+
}
261266
}
262267

268+
// Raise regardless to cover scenario where size needs to be recalculated if the folder is manually removed on disk.
263269
OnPropertyChanged(nameof(CacheFolderSize));
264270

265271
return success;

0 commit comments

Comments
 (0)