memstechtips/Winhance · error · InvalidOperationException
Could not clear existing working directory: {cleanupEx.Messa
Error message
Could not clear existing working directory: {cleanupEx.Message} What it means
The catch-all around the working-directory cleanup block in IsoService. Any exception that is not OperationCanceledException or the explicit InvalidOperationException from error [0] is rewrapped as a new InvalidOperationException with the original message. Its real value is preserving the inner exception via the `cleanupEx` constructor argument so the stack trace is not lost.
Source
Thrown at src/Winhance.Infrastructure/Features/AdvancedTools/Services/IsoService.cs:135
"It may be open in Windows Explorer or being used by another process. " +
"Please close it or delete it manually and try again."
);
}
_logService.LogInformation("Working directory cleared successfully");
}
catch (OperationCanceledException)
{
throw;
}
catch (InvalidOperationException)
{
throw;
}
catch (Exception cleanupEx)
{
_logService.LogError($"Failed to clear working directory: {cleanupEx.Message}", cleanupEx);
throw new InvalidOperationException($"Could not clear existing working directory: {cleanupEx.Message}", cleanupEx);
}
}
_fileSystemService.CreateDirectory(workingDirectory);
progress?.Report(new TaskProgressDetail
{
StatusText = _localization.GetString("Progress_MountingIso"),
TerminalOutput = $"ISO: {isoPath}"
});
_logService.LogInformation($"Mounting ISO: {isoPath}");
var mountResult = await _processExecutor.ExecuteAsync(
"powershell.exe",
$"-NoProfile -Command \"(Mount-DiskImage -ImagePath '{isoPath}' -PassThru | Get-Volume).DriveLetter\"",
cancellationToken).ConfigureAwait(false);
var rawOutput = mountResult.StandardOutput;
View on GitHub (pinned to f23d554eb2)
Solutions
- Inspect the InnerException (and the log line just before the throw) to find the real cause — the wrapper message only echoes cleanupEx.Message.
- If powershell.exe launch failed, verify PowerShell is installed and not blocked by group policy, then retry.
- Reproduce with verbose logging enabled to capture the full stack of the inner exception.
- Treat the cleanup as best-effort: consider relaxing the outer caller so a cleanup failure does not abort the entire ISO workflow when the directory can be recreated fresh.
Example fix
// before: blanket rewrap that hides the original type
throw new InvalidOperationException($"Could not clear existing working directory: {cleanupEx.Message}", cleanupEx);
// after: keep the original exception type when it is already actionable, only wrap unknowns
if (cleanupEx is IOException or UnauthorizedAccessException or System.ComponentModel.Win32Exception)
throw; // callers already know how to handle these
throw new InvalidOperationException($"Could not clear existing working directory: {cleanupEx.Message}", cleanupEx); Defensive patterns
Strategy: try-catch
Try / catch
catch (InvalidOperationException ex) when (ex.Message.StartsWith("Could not clear existing working directory"))
{
// Inspect ex.InnerException for the real cause; report InnerException.GetType().Name + Message to the user.
// Do not retry blindly — the inner exception determines whether a retry is safe.
} Prevention
- Always read InnerException on these wrapped errors — the message is just the inner one.
- Enable verbose logging so the original stack is captured before rewrapping.
- Treat working-directory cleanup as best-effort where possible so a transient failure does not abort the workflow.
When it happens
Trigger: An unexpected exception type escapes the inner try during cleanup — e.g. Remove-Item throws a PowerShell process-launch failure, _processExecutor.ExecuteAsync throws IOException, the cancellation token surfaces as a non-OCE exception, or DirectoryExists itself throws UnauthorizedAccessException.
Common situations: powershell.exe is not on PATH or is blocked by policy. The file system abstraction throws on a transient I/O error. A third-party shell replacement interferes with launching powershell.exe. A bug in a downstream service throws an exception type the author did not anticipate.
Related errors
- Could not delete the existing working directory '{workingDir
- Insufficient disk space on {driveName} for {operationName}.
AI-assisted analysis of memstechtips/Winhance@f23d554eb2 (2026-08-13).
Data as JSON: /api/errors/2621f2101ddc3546.
Report an issue: GitHub.