public static Int64 DirectorySize(this string folderPath) { return(SystemIOFileHelpers.EnrichIoException(folderPath, () => folderPath.DirectoryFilesSearchAllDirectories("*.*") .Select(fn => SystemIOFileHelpers.FileSize(fn)) .Sum())); }
public static bool DirectoryExists(this string pathToTest) { return(SystemIOFileHelpers.EnrichIoException(pathToTest, () => { if (Directory.Exists(pathToTest)) { return true; } // If d.e says no, we should check whether it's because it can't tell, or because it can't determine if (System.IO.File.Exists(pathToTest)) { return false; } try { // Try to get the file listing of the dir. pathToTest.DirectoryFiles("_test_"); return true; } catch (DirectoryNotFoundException) { return false; } } )); }
public static void CreateDirectory(this string directory) { if (!directory.DirectoryExists()) { SystemIOFileHelpers.EnrichIoException(directory, () => { Directory.CreateDirectory(directory); }); } }
public static void MoveFileOrDirectory(this string filePath, string targetPath) { SystemIOFileHelpers.EnrichIoException(filePath, targetPath, () => { targetPath.GetDirectoryName().CreateDirectory(); if (filePath.FileExists()) { System.IO.File.Move(filePath, targetPath); } else if (filePath.DirectoryExists()) { MoveDirectory(filePath, targetPath); } }); }
public static void DeleteDirectoryIfEmpty(this string dir) { SystemIOFileHelpers.EnrichIoException(dir, () => { if (dir.DirectoryExists()) { Directory.Delete(dir); if (dir.DirectoryExists()) { throw new IOException( "Directory.Delete passed, but the directory still exists") .AddPath(dir); } } }); }
public static void MoveDirectory(this string sourcePath, string targetPath) { SystemIOFileHelpers.EnrichIoException(sourcePath, targetPath, () => { var sourceRoot = sourcePath.GetPathRoot(); var targetRoot = targetPath.GetPathRoot(); targetPath.GetDirectoryName().CreateDirectory(); if (sourceRoot.Equals(targetRoot)) { Directory.Move(sourcePath, targetPath); } else { MoveDirectoryWithDifferentRoots(sourcePath, targetPath); } }); }
public static void CopyDirectory(this string srcDirectoryPath, string targetDirectoryPath) { SystemIOFileHelpers.EnrichIoException(srcDirectoryPath, targetDirectoryPath, () => { targetDirectoryPath.CreateDirectory(); foreach (var path in Directory.GetDirectories(srcDirectoryPath)) { var destDir = path.Replace(srcDirectoryPath, targetDirectoryPath); path.CopyDirectory(destDir); } foreach ( var path in srcDirectoryPath.DirectoryFiles()) { var destPath = path.Replace(srcDirectoryPath, targetDirectoryPath); path.CopyFile(destPath); } }); }
public static bool IsDirectory(this string path) { return(SystemIOFileHelpers.EnrichIoException(path, () => path.DirectoryExists())); }