/// <summary> /// Ensures that all directories of a fullPath exist creating them as needed. /// Returns false if fullPath did not supply valid path /// </summary> public static bool EnsureDirectoryPath(this FileSystemSession session, string fullPath) { var segments = session.NonNull(nameof(session)).FileSystem.SplitPathSegments(fullPath); if (segments == null || segments.Length == 0) { return(false); } //set to root var dir = session[session.FileSystem.GetPathRoot(fullPath)] as FileSystemDirectory; if (dir == null) { return(false); } foreach (var seg in segments) { var sub = dir.GetSubDirectory(seg); if (sub == null) { sub = dir.CreateDirectory(seg); } dir = sub; } return(true); }
/// <summary> /// Ensures that all directories of a fullPath exist creating them as needed. /// Returns false if fullPath did not supply valid path /// </summary> public static bool EnsureDirectoryPath(this FileSystemSession session, string fullPath) { var segments = session.NonNull(nameof(session)).FileSystem.SplitPathSegments(fullPath); if (segments == null || segments.Length == 0) { return(false); } //set to root var dir = session[session.FileSystem.GetPathRoot(fullPath)] as FileSystemDirectory; if (dir == null) { return(false); } var toClean = new List <FileSystemSessionItem> { dir }; try { foreach (var seg in segments) { var sub = dir.GetSubDirectory(seg); if (sub == null) { sub = dir.CreateDirectory(seg); } toClean.Add(sub); dir = sub; } } finally { toClean.ForEach(i => i.Dispose()); } return(true); }
/// <summary> /// Opens existing or creates a new file /// </summary> public static FileSystemFile OpenOrCreateFile(this FileSystemSession session, string fullPath) { var(path, fileName) = session.NonNull(nameof(session)) .FileSystem .ExtractFilePathAndName(fullPath.NonBlank(nameof(fullPath))); var dir = session[path] as FileSystemDirectory; if (dir == null) { throw new FileSystemException(StringConsts.FS_DIRECTORY_DOES_NOT_EXIST_ERROR.Args(path.TakeLastChars(32, "..."))); } var file = dir[fileName] as FileSystemFile; if (file == null) { file = dir.CreateFile(fileName); } return(file); }