Index: Core/Common/src/Core.Common.IO/DirectoryHelper.cs =================================================================== diff -u --- Core/Common/src/Core.Common.IO/DirectoryHelper.cs (revision 0) +++ Core/Common/src/Core.Common.IO/DirectoryHelper.cs (revision d70be6f1085d61194caa344db80ba4938fd08218) @@ -0,0 +1,80 @@ +// Copyright (C) Stichting Deltares 2021. All rights reserved. +// +// This file is part of Riskeer. +// +// Riskeer is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this program. If not, see . +// +// All names, logos, and references to "Deltares" are registered trademarks of +// Stichting Deltares and remain full property of Stichting Deltares at all times. +// All rights reserved. + +using System; +using System.IO; +using System.Threading; + +namespace Core.Common.IO +{ + /// + /// Class containing helper methods for correctly dealing with directories. + /// + public static class DirectoryHelper + { + /// + /// Tries to delete the provided directory. + /// + /// The path of the directory to delete. + /// The number of retries in case of an . + /// This helper method is a solution to latency issues caused by temporary file locks. + /// Thrown when: + /// + /// a file with the same name and location specified by path exists; + /// the directory specified by path is read-only; + /// the directory is the application's current working directory; + /// the directory is the application's current working directory; + /// the directory contains a read-only file; + /// the directory is being used by another process. + /// + /// + /// Thrown when the caller does not have the required permission. + /// Thrown when path is a zero-length string, contains only white space, or contains one or more invalid characters. + /// Thrown when path is null. + /// Thrown when the specified path exceeds the system-defined maximum length. + /// Thrown when: + /// + /// path does not exist or could not be found; + /// the specified path is invalid (for example, it is on an unmapped drive). + /// + /// + public static void TryDelete(string path, int numberOfRetries = 5) + { + try + { + Directory.Delete(path, true); + } + catch (IOException) + { + if (numberOfRetries != 0) + { + Thread.Sleep(100); + + TryDelete(path, numberOfRetries - 1); + } + else + { + throw; + } + } + } + } +} \ No newline at end of file Index: Core/Common/test/Core.Common.TestUtil/Core.Common.TestUtil.csproj =================================================================== diff -u -ra0c510a8873c29a3f58fe45351565ea7ee72a778 -rd70be6f1085d61194caa344db80ba4938fd08218 --- Core/Common/test/Core.Common.TestUtil/Core.Common.TestUtil.csproj (.../Core.Common.TestUtil.csproj) (revision a0c510a8873c29a3f58fe45351565ea7ee72a778) +++ Core/Common/test/Core.Common.TestUtil/Core.Common.TestUtil.csproj (.../Core.Common.TestUtil.csproj) (revision d70be6f1085d61194caa344db80ba4938fd08218) @@ -21,6 +21,7 @@ + Index: Core/Common/test/Core.Common.TestUtil/DirectoryDisposeHelper.cs =================================================================== diff -u -r3d088548dfdebfd5c3adbbd67075b42e134f5c03 -rd70be6f1085d61194caa344db80ba4938fd08218 --- Core/Common/test/Core.Common.TestUtil/DirectoryDisposeHelper.cs (.../DirectoryDisposeHelper.cs) (revision 3d088548dfdebfd5c3adbbd67075b42e134f5c03) +++ Core/Common/test/Core.Common.TestUtil/DirectoryDisposeHelper.cs (.../DirectoryDisposeHelper.cs) (revision d70be6f1085d61194caa344db80ba4938fd08218) @@ -23,7 +23,7 @@ using System.IO; using System.Linq; using System.Security.AccessControl; -using System.Threading; +using Core.Common.IO; namespace Core.Common.TestUtil { @@ -42,7 +42,6 @@ /// public class DirectoryDisposeHelper : IDisposable { - private const int numberOfAdditionalDeleteAttempts = 3; private readonly string rootPathToTemp; private readonly string fullPath; private bool disposed; @@ -124,37 +123,11 @@ directoryPermissionsRevoker?.Dispose(); } - var attempts = 0; - while (!TryDeleteRootFolder() && attempts < numberOfAdditionalDeleteAttempts) - { - attempts++; + DirectoryHelper.TryDelete(rootPathToTemp); - GC.WaitForPendingFinalizers(); - Thread.Sleep(10); - } - disposed = true; } - private bool TryDeleteRootFolder() - { - try - { - Directory.Delete(rootPathToTemp, true); - } - catch (Exception e) - { - if (e is IOException) - { - return false; - } - - // Ignore other exceptions - } - - return true; - } - /// /// Creates the temporary folder path. /// Index: Riskeer/MacroStabilityInwards/src/Riskeer.MacroStabilityInwards.IO/Exporters/MacroStabilityInwardsCalculationGroupExporter.cs =================================================================== diff -u -r70f7bc2ec3c640350e2525c888837254657d6315 -rd70be6f1085d61194caa344db80ba4938fd08218 --- Riskeer/MacroStabilityInwards/src/Riskeer.MacroStabilityInwards.IO/Exporters/MacroStabilityInwardsCalculationGroupExporter.cs (.../MacroStabilityInwardsCalculationGroupExporter.cs) (revision 70f7bc2ec3c640350e2525c888837254657d6315) +++ Riskeer/MacroStabilityInwards/src/Riskeer.MacroStabilityInwards.IO/Exporters/MacroStabilityInwardsCalculationGroupExporter.cs (.../MacroStabilityInwardsCalculationGroupExporter.cs) (revision d70be6f1085d61194caa344db80ba4938fd08218) @@ -23,10 +23,10 @@ using System.Collections.Generic; using System.IO; using System.Linq; -using System.Threading; using Components.Persistence.Stability; using Core.Common.Base.Data; using Core.Common.Base.IO; +using Core.Common.IO; using Core.Common.IO.Exceptions; using Core.Common.Util; using log4net; @@ -138,7 +138,7 @@ { if (Directory.Exists(tempFolderPath)) { - DeleteDirectory(tempFolderPath); + DirectoryHelper.TryDelete(tempFolderPath); } } } @@ -229,22 +229,5 @@ var fileNameWithExtension = $"{fileName}.{fileExtension}"; return Path.Combine(currentFolderPath, fileNameWithExtension); } - - private static void DeleteDirectory(string directory, int numberOfRetries = 5) - { - try - { - Directory.Delete(directory, true); - } - catch (IOException) - { - Thread.Sleep(1000); - - if (numberOfRetries != 0) - { - DeleteDirectory(directory, numberOfRetries - 1); - } - } - } } } \ No newline at end of file