From cc7d05a4a6d0f1aa11be5e911fcb45023f755cb6 Mon Sep 17 00:00:00 2001 From: Mitchell Paulus Date: Tue, 13 Aug 2024 08:59:36 -0500 Subject: [PATCH] Better handle IO exceptions when file held by another process --- excelchop/Program.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/excelchop/Program.cs b/excelchop/Program.cs index a835e83..7b7cb02 100644 --- a/excelchop/Program.cs +++ b/excelchop/Program.cs @@ -98,6 +98,19 @@ static bool OptionMatches(string arg, IOption option) Environment.ExitCode = 1; Console.Error.Write(knownException.Message.EndWithSingleNewline()); } + catch (IOException ioException) + { + if (ioException.Message.Contains("The process cannot access the file") && ioException.Message.Contains("because it is being used by another process")) + { + Console.Error.Write(ioException.Message); + if (!ioException.Message.EndsWith('\n')) Console.Error.Write('\n'); + Environment.ExitCode = 1; + return; + } + + // Else rethrow. + throw; + } catch { Environment.ExitCode = 1;