// Copyright (C) Stichting Deltares 2016. All rights reserved. // // This file is part of Ringtoets. // // Ringtoets 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.Linq; using Core.Common.Utils.Builders; using Core.Common.Utils.Properties; namespace Core.Common.Utils { /// /// Class with reusable File related utility methods. /// public static class FileUtils { /// /// Validates the file path. /// /// The file path to be validated. /// is invalid. /// A valid path: /// /// is not empty or null, /// does not consist out of only whitespace characters, /// does not contain an invalid character, /// does not end with a directory or path separator (empty file name). /// public static void ValidateFilePath(string path) { if (String.IsNullOrWhiteSpace(path)) { var message = new FileReaderErrorMessageBuilder(path).Build(Resources.Error_Path_must_be_specified); throw new ArgumentException(message); } string name; try { name = Path.GetFileName(path); } catch (ArgumentException e) { var message = new FileReaderErrorMessageBuilder(path) .Build(String.Format(Resources.Error_Path_cannot_contain_Characters_0_, String.Join(", ", Path.GetInvalidFileNameChars()))); throw new ArgumentException(message, e); } if (String.Empty == name) { var message = new FileReaderErrorMessageBuilder(path).Build(Resources.Error_Path_must_not_point_to_empty_file_name); throw new ArgumentException(message); } } /// /// Validates the file path. /// /// The file path to be validated. /// true if the file path is valid, false otherwise. /// A valid path: /// /// contains not only whitespace, /// does not contain an invalid character, /// is not empty or null, /// does not end with a directory or path separator (empty file name). /// public static bool IsValidFilePath(string path) { try { ValidateFilePath(path); } catch (ArgumentException) { return false; } return true; } /// /// Searches the files in that match and /// deletes the files older than days. /// /// The directory to search. /// The search string to match against the names of files in path. /// The maximum number days since the creation of the file. /// Thrown when of is null, is a zero-length string, /// contains only white space, or contains one or more invalid characters. /// Thrown when an error occured trying to search and delete files in . public static void DeleteOldFiles(string path, string searchPattern, int numberOfDaysToKeepFiles) { if (string.IsNullOrWhiteSpace(path)) { throw new ArgumentException(@"No valid value for 'path'.", "path"); } if (string.IsNullOrWhiteSpace(searchPattern)) { throw new ArgumentException(@"No valid value for 'searchPattern'.", "searchPattern"); } try { foreach (string logFile in Directory.GetFiles(path, searchPattern).Where( l => (DateTime.Now - File.GetCreationTime(l)).TotalDays > numberOfDaysToKeepFiles)) { File.Delete(logFile); } } catch (Exception e) { if (e is ArgumentException || e is IOException || e is NotSupportedException || e is UnauthorizedAccessException) { var message = string.Format(Resources.FileUtils_DeleteOldFiles_Error_occured_deleting_files_in_folder_0, path); throw new IOException(message, e); } throw; } } } }