From 540f14da0d2017cd55c274248a36f254ae840031 Mon Sep 17 00:00:00 2001 From: Pilzinsel64 Date: Fri, 11 Oct 2024 11:19:08 +0200 Subject: [PATCH] safe-fail if old file can't be deleted --- Pilz.Updating.Client/Utils.cs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/Pilz.Updating.Client/Utils.cs b/Pilz.Updating.Client/Utils.cs index 4dc4b96..219d0ab 100644 --- a/Pilz.Updating.Client/Utils.cs +++ b/Pilz.Updating.Client/Utils.cs @@ -25,6 +25,7 @@ public static class Utils var triesLeft = 1; var srcFileName = sourceFile.FullName; var destFileName = destinationFile.FullName; + string? oldFile = null; while (triesLeft > 0) { @@ -38,9 +39,8 @@ public static class Utils { if (triesLeft == 0 && File.Exists(destFileName)) { - var oldFile = destFileName + ".old"; + oldFile = destFileName + ".old"; File.Move(destFileName, oldFile, true); - File.Delete(oldFile); triesLeft++; } } @@ -48,5 +48,16 @@ public static class Utils { } } + + if (oldFile != null) + { + try + { + File.Delete(oldFile); + } + catch (Exception) + { + } + } } }