Skip to content

Free, open-source Windows optimization tool for performance, privacy, and simplicity.

fix(SystemHelper): add try catch for CheckForExclusions and AddToExclusions

itsfatduckbddbbaf49784
diff --git a/optimizerDuck/src/Core/Helpers/SystemHelper.cs b/optimizerDuck/src/Core/Helpers/SystemHelper.cs
index 1c56bf1..ac98697 100644
--- a/optimizerDuck/src/Core/Helpers/SystemHelper.cs
+++ b/optimizerDuck/src/Core/Helpers/SystemHelper.cs
@@ -43,36 +43,51 @@ public static class SystemHelper
{
var exePath = Defaults.ExePath;
var rootPath = Defaults.RootPath;
-
- var result = ShellService.PowerShell($$"""
+ try
+ {
+ var result = ShellService.PowerShell($$"""
@{
SelfPath = (Get-MpPreference).ExclusionPath -contains "{{exePath}}";
RootPath = (Get-MpPreference).ExclusionPath -contains "{{rootPath}}"
} | ConvertTo-Json
""");
- var exclusions = JsonConvert.DeserializeObject<Dictionary<string, bool>>(result.Stdout);
- var missingPaths = new List<string>();
- if (exclusions != null && exclusions.TryGetValue("SelfPath", out var selfPathExcluded) && !selfPathExcluded)
- {
- Log.LogWarning("Path not excluded: {SelfPath}", exePath);
- missingPaths.Add(exePath);
- }
+ var exclusions = JsonConvert.DeserializeObject<Dictionary<string, bool>>(result.Stdout);
+ var missingPaths = new List<string>();
+ if (exclusions != null && exclusions.TryGetValue("SelfPath", out var selfPathExcluded) && !selfPathExcluded)
+ {
+ Log.LogWarning("Path not excluded: {SelfPath}", exePath);
+ missingPaths.Add(exePath);
+ }
- if (exclusions != null && exclusions.TryGetValue("RootPath", out var rootPathExcluded) && !rootPathExcluded)
+ if (exclusions != null && exclusions.TryGetValue("RootPath", out var rootPathExcluded) && !rootPathExcluded)
+ {
+ Log.LogWarning("Path not excluded: {RootPath}", rootPath);
+ missingPaths.Add(rootPath);
+ }
+ return missingPaths;
+ }
+ catch (Exception ex)
{
- Log.LogWarning("Path not excluded: {RootPath}", rootPath);
- missingPaths.Add(rootPath);
+ Log.LogError(ex, "Failed to check for exclusions.");
}
- return missingPaths;
+
+ return [];
}
public static void AddToExclusions(List<string> paths)
{
- using var tracker = ServiceTracker.Begin(Log);
- foreach (var path in paths)
+ try
+ {
+ using var tracker = ServiceTracker.Begin(Log);
+ foreach (var path in paths)
+ {
+ var result = ShellService.PowerShell($"Add-MpPreference -ExclusionPath \"{path}\"");
+ if (result.ExitCode != 0) Log.LogError($"[{Theme.Error}]Failed to add path to exclusions: {path}[/]");
+ }
+ }
+ catch (Exception ex)
{
- var result = ShellService.PowerShell($"Add-MpPreference -ExclusionPath \"{path}\"");
- if (result.ExitCode != 0) Log.LogError($"[{Theme.Error}]Failed to add path to exclusions: {path}[/]");
+ Log.LogError(ex, "Failed to add paths to exclusions.");
}
}
}
\ No newline at end of file