From 16a96329b549f749f4e4820a11d2cb9540c7099e Mon Sep 17 00:00:00 2001 From: Pilzinsel64 Date: Wed, 4 Sep 2024 11:24:20 +0200 Subject: [PATCH] use safe copy file on single file updating --- Pilz.Updating.Client/UpdateClient.cs | 2 +- Pilz.Updating.Client/Utils.cs | 55 +++++++++++++++------------- 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/Pilz.Updating.Client/UpdateClient.cs b/Pilz.Updating.Client/UpdateClient.cs index 61cac6a..f9e676b 100644 --- a/Pilz.Updating.Client/UpdateClient.cs +++ b/Pilz.Updating.Client/UpdateClient.cs @@ -190,7 +190,7 @@ public class UpdateClient(string updateUrl, AppVersion currentVersion, AppChanne Utils.CopyFiles(dataPathDir, destDir); } else if (package.UpdateType == UpdateType.File) - File.Copy(dataPath, localInstallPath); + Utils.CopyFile(new FileInfo(dataPath), new FileInfo(localInstallPath)); RaiseStatusChanged(UpdateStatus.Copying, UpdateStatusEvent.PostEvent); // Delete Package diff --git a/Pilz.Updating.Client/Utils.cs b/Pilz.Updating.Client/Utils.cs index 32b46d5..2cb8609 100644 --- a/Pilz.Updating.Client/Utils.cs +++ b/Pilz.Updating.Client/Utils.cs @@ -10,31 +10,7 @@ public static class Utils foreach (FileInfo sFile in sourceDir.EnumerateFiles("*", SearchOption.TopDirectoryOnly)) { var dFile = new FileInfo(Path.Combine(destinationDir.FullName, sFile.Name)); - var triesLeft = 1; - - while (triesLeft > 0) - { - triesLeft--; - - try - { - sFile.CopyTo(dFile.FullName, true); - } - catch (IOException) - { - if (triesLeft == 0 && File.Exists(dFile.FullName)) - { - var oldFile = dFile.FullName + ".old"; - File.Delete(oldFile); - File.Move(dFile.FullName, oldFile, true); - File.Delete(oldFile); - triesLeft++; - } - } - catch (Exception) - { - } - } + CopyFile(sFile, dFile); } foreach (DirectoryInfo sDir in sourceDir.EnumerateDirectories("*", SearchOption.TopDirectoryOnly)) @@ -43,4 +19,33 @@ public static class Utils CopyFiles(sDir, dDir); } } + + public static void CopyFile(FileInfo sourceFile, FileInfo destinationFile) + { + var triesLeft = 1; + + while (triesLeft > 0) + { + triesLeft--; + + try + { + sourceFile.CopyTo(destinationFile.FullName, true); + } + catch (IOException) + { + if (triesLeft == 0 && File.Exists(destinationFile.FullName)) + { + var oldFile = destinationFile.FullName + ".old"; + File.Delete(oldFile); + File.Move(destinationFile.FullName, oldFile, true); + File.Delete(oldFile); + triesLeft++; + } + } + catch (Exception) + { + } + } + } }