/* INFINITY CODE */ /* https://infinity-code.com */ using System; using System.IO; using System.Linq; using System.Threading; namespace InfinityCode.RealWorldTerrain { /// /// Provides utility methods for file system operations. /// public static class RealWorldTerrainFileSystem { /// /// The number of bytes in megabyte. /// public const int MB = 1048576; /// /// Calculates the total size of a directory in bytes. /// /// The directory for which to calculate the size. /// The total size of the directory in bytes. public static long GetDirectorySize(DirectoryInfo folder) { return folder.GetFiles().Sum(fi => fi.Length) + folder.GetDirectories().Sum(dir => GetDirectorySize(dir)); } /// /// Calculates the total size of a directory in bytes. /// /// The path of the directory for which to calculate the size. /// The total size of the directory in bytes. public static long GetDirectorySize(string folderPath) { return GetDirectorySize(new DirectoryInfo(folderPath)); } /// /// Calculates the total size of a directory in megabytes. /// /// The path of the directory for which to calculate the size. /// The total size of the directory in megabytes. public static long GetDirectorySizeMB(string folderPath) { return GetDirectorySize(folderPath) / MB; } /// /// Safely deletes a directory. /// /// The name of the directory to delete. public static void SafeDeleteDirectory(string directoryName) { try { Directory.Delete(directoryName, true); } catch { } } /// /// Safely deletes a file. /// /// The path of the file to delete. /// The number of attempts to delete the file. Default is 10. public static void SafeDeleteFile(string filename, int tryCount = 10) { while (tryCount-- > 0) { try { File.Delete(filename); break; } catch (Exception) { #if !NETFX_CORE Thread.Sleep(10); #endif } } } } }