Skip to content

Commit 2368e3f

Browse files
Jack251970TBM13
authored andcommitted
Merge pull request Flow-Launcher#3992 from Flow-Launcher/clear_cache_twice
Fix DirectoryNotFoundException when deleting cache twice
1 parent f4e19a6 commit 2368e3f

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
@@ -100,35 +100,41 @@ private bool ClearCacheFolder()
100100
}
101101
});
102102

103-
// Firstly, delete plugin cache directories
104-
pluginCacheDirectory.EnumerateDirectories("*", SearchOption.TopDirectoryOnly)
105-
.ToList()
106-
.ForEach(dir =>
107-
{
108-
try
109-
{
110-
// Plugin may create directories in its cache directory
111-
dir.Delete(recursive: true);
112-
}
113-
catch (Exception e)
114-
{
115-
App.API.LogException(ClassName, $"Failed to delete cache directory: {dir.Name}", e);
116-
success = false;
117-
}
118-
});
119-
120-
// Then, delete plugin directory
121-
var dir = GetPluginCacheDir();
122-
try
103+
// Check if plugin cache directory exists before attempting to delete
104+
// Or it will throw DirectoryNotFoundException in `pluginCacheDirectory.EnumerateDirectories`
105+
if (pluginCacheDirectory.Exists)
123106
{
124-
dir.Delete(recursive: false);
125-
}
126-
catch (Exception e)
127-
{
128-
App.API.LogException(ClassName, $"Failed to delete cache directory: {dir.Name}", e);
129-
success = false;
107+
// Firstly, delete plugin cache directories
108+
pluginCacheDirectory.EnumerateDirectories("*", SearchOption.TopDirectoryOnly)
109+
.ToList()
110+
.ForEach(dir =>
111+
{
112+
try
113+
{
114+
// Plugin may create directories in its cache directory
115+
dir.Delete(recursive: true);
116+
}
117+
catch (Exception e)
118+
{
119+
App.API.LogException(ClassName, $"Failed to delete cache directory: {dir.Name}", e);
120+
success = false;
121+
}
122+
});
123+
124+
// Then, delete plugin directory
125+
var dir = pluginCacheDirectory;
126+
try
127+
{
128+
dir.Delete(recursive: false);
129+
}
130+
catch (Exception e)
131+
{
132+
App.API.LogException(ClassName, $"Failed to delete cache directory: {dir.Name}", e);
133+
success = false;
134+
}
130135
}
131136

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

134140
return success;

0 commit comments

Comments
 (0)