// Copyright (C) Stichting Deltares 2022. 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.Util { /// /// Class containing helper methods for correctly dealing with directories. /// public static class DirectoryHelper { /// /// Tries to delete the directory specified by . /// /// The path of the directory to delete. /// The number of retries in case an occurs. /// This helper method is a solution to latency issues caused by file locks, which normally cause an /// . The file locks at stake will be released as soon as the application becomes idle, /// which explains retrying the delete action for times. /// Thrown when: /// /// a file with the same name and location exists; /// the directory is read-only; /// 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 permissions. /// Thrown when the specified path is a zero-length string, contains only white spaces, or contains one or more invalid characters. /// Thrown when is null. /// Thrown when the specified path exceeds the system defined maximum length. /// Thrown when the specified path: /// /// does not exist or could not be found; /// 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; } } } } }