/// <summary> /// Copies the specified source folder to the destination. /// </summary> /// <param name="source">The source.</param> /// <param name="destination">The destination.</param> /// <param name="overwriteFolder">if set to <c>true</c> overwrites the destination folder if it exists.</param> /// <param name="overwriteItems">if set to <c>true</c> overwrites any existing items with the same name in the destination folder.</param> /// <returns>Whether the folder was copied or not.</returns> public static bool Copy(AFolder source, AFolder destination, bool overwriteFolder = false, bool overwriteItems = false) { Exceptions.NotNullException<AFolder>(source, nameof(source)); Exceptions.NotNullException<AFolder>(destination, nameof(destination)); if (!overwriteFolder && Fenrir.FileSystem.FolderExists(destination.FullPath)) return false; FileCollisionOption itemCollision; if (overwriteItems) itemCollision = FileCollisionOption.ReplaceExisting; else itemCollision = FileCollisionOption.FailIfExists; return source.Copy(destination.FullPath, FolderCollisionOption.ReplaceExisting, itemCollision) != null; }
/// <summary> /// Gets a folder. /// </summary> /// <param name="directory">The directory.</param> /// <param name="folder">The folder.</param> /// <param name="openMode">The open mode. Defaults to Normal.</param> /// <returns>The folder.</returns> public override AFolder OpenFolder(AFolder directory, string folder, OpenMode openMode) { string path = FSHelpers.CombinePath(directory.ToString(), folder); if (!Fenrir.FileSystem.FolderExists(path)) { switch (openMode) { case OpenMode.Normal: return Fenrir.FileSystem.CreateFolder(path, FileCollisionOption.FailIfExists); case OpenMode.FailIfDoesNotExist: throw new FileNotFoundException(String.Format("Folder {0} does not exist!", path)); } } return new FenrirFolder(path); }
/// <summary> /// Opens a folder asynchronously. /// </summary> /// <param name="directory">The directory.</param> /// <param name="folder">The folder.</param> /// <param name="openMode">The open mode. Defaults to Normal.</param> /// <param name="cancellationToken">The cancellation token. Defaults to null.</param> /// <returns>An AFolder task to open a folder. The AFolder represents the folder.</returns> public async Task<AFolder> OpenFolderAsync(AFolder directory, string folder, OpenMode openMode, CancellationToken? cancellationToken = null) { await AwaitHelpers.CreateTaskScheduler(AwaitHelpers.CheckCancellationToken(cancellationToken)); return OpenFolder(directory, folder, openMode); }
/// <summary> /// Gets a file. /// </summary> /// <param name="directory">The directory.</param> /// <param name="file">The file.</param> /// <param name="openMode">The open mode Defaults to Normal..</param> /// <returns>The file.</returns> /// <exception cref="System.NotImplementedException"> /// Exception representing that this function is not implemented. /// </exception> public virtual AFile OpenFile(AFolder directory, string file, OpenMode openMode = OpenMode.Normal) { throw new NotImplementedException(); }
/// <summary> /// Generates a unique name for a folder. /// </summary> /// <param name="directory">The directory.</param> /// <param name="name">The name.</param> /// <param name="iterations">The maximum iterations. Defaults to 99.</param> /// <returns>A new unique name for the folder.</returns> public virtual string GenerateFolderUniqueName(AFolder directory, string name, int iterations = 99) { string basename = Path.GetFileNameWithoutExtension(name); string ext = Path.GetExtension(name); StringBuilder str = new StringBuilder(); for (int i = 0; i < iterations; i++) { switch (i) { case 0: if (!FolderExists(Path.Combine(directory.ToString(), name))) return name; break; default: str.Clear(); str.Append(String.Format("{0} - ({1})", basename, i)); if (!FolderExists(Path.Combine(directory.ToString(), str.ToString()))) return str.ToString(); break; } } throw Exceptions.CanNotGenerateUniqueNameException(iterations); }
/// <summary> /// Asynchronously returns an string list of all folders in the folder that match the search pattern and are within the search option choosen. /// </summary> /// <param name="folder">The folder.</param> /// <param name="searchPattern">The search pattern.</param> /// <param name="searchOption">The search option.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>A string list task to get the folders. The string list represents the names of all folders in the folder.</returns> public static async Task<List<string>> GetFoldersListAsync(AFolder folder, string searchPattern = "*", SearchOption searchOption = SearchOption.AllDirectories, CancellationToken? cancellationToken = null) { await AwaitHelpers.CreateTaskScheduler(AwaitHelpers.CheckCancellationToken(cancellationToken)); return GetFoldersList(folder, searchPattern, searchOption); }
/// <summary> /// Gets the UTC creation time of the folder. /// </summary> /// <param name="folder">The folder.</param> /// <returns>The creation time of the folder.</returns> public static DateTime GetCreationTimeUtc(AFolder folder) { Exceptions.NotNullException<AFolder>(folder, nameof(folder)); return folder.CreationTimeUtc; }
/// <summary> /// Asynchronously gets the root folder of the folder. /// </summary> /// <param name="folder">The folder.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>A string task to get the root folder. The string is the root folder.</returns> public static async Task<string> GetRootFolderAsync(AFolder folder, CancellationToken? cancellationToken = null) { await AwaitHelpers.CreateTaskScheduler(AwaitHelpers.CheckCancellationToken(cancellationToken)); return GetRootFolder(folder); }
/// <summary> /// Moves the specified folder to the destination. /// </summary> /// <param name="source">The source.</param> /// <param name="destination">The destination.</param> /// <param name="overwriteFolder">if set to <c>true</c> overwrites the destination folder if it exists.</param> /// <returns>Whether the folder was moved or not.</returns> public static bool Move(AFolder source, AFolder destination, bool overwriteFolder = false) { Exceptions.NotNullException<AFolder>(source, nameof(source)); Exceptions.NotNullException<AFolder>(destination, nameof(destination)); if (!overwriteFolder && Fenrir.FileSystem.FolderExists(destination.FullPath)) return false; return source.Move(destination.FullPath, FolderCollisionOption.ReplaceExisting); }
/// <summary> /// Gets the parent folder of the folder. /// </summary> /// <param name="folder">The folder.</param> /// <returns>The parent folder.</returns> public static string GetParentFolder(AFolder folder) { return folder.ParentFolder; }
/// <summary> /// Gets the root folder of the folder. /// </summary> /// <param name="folder">The folder.</param> /// <returns>The root folder.</returns> public static string GetRootFolder(AFolder folder) { return folder.RootFolder; }
/// <summary> /// Asynchronously copies the specified source folder to the destination. /// </summary> /// <param name="source">The source.</param> /// <param name="destination">The destination.</param> /// <param name="overwriteFolder">if set to <c>true</c> overwrites the destination folder if it exists.</param> /// <param name="overwriteItems">if set to <c>true</c> overwrites any existing items with the same name in the destination folder.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>A boolean task to copy the folder. The boolean represents whether the folder was copied or not.</returns> public static async Task<bool> CopyAsync(AFolder source, string destination, bool overwriteFolder = false, bool overwriteItems = false, CancellationToken? cancellationToken = null) { await AwaitHelpers.CreateTaskScheduler(AwaitHelpers.CheckCancellationToken(cancellationToken)); return Copy(source, destination, overwriteFolder, overwriteItems); }
/// <summary> /// Gets the UTC last modified time of the folder. /// </summary> /// <param name="folder">The folder.</param> /// <returns>The last modified time of the folder.</returns> public static DateTime GetLastModifiedTimeUtc(AFolder folder) { Exceptions.NotNullException<AFolder>(folder, nameof(folder)); return folder.LastModifiedTimeUtc; }
/// <summary> /// Gets the last accessed time of the folder. /// </summary> /// <param name="folder">The folder.</param> /// <returns>The last accessed time of the folder.</returns> public static DateTime GetLastAccessedTime(AFolder folder) { Exceptions.NotNullException<AFolder>(folder, nameof(folder)); return folder.LastAccessedTime; }
/// <summary> /// Returns whether the folder exists or not. /// </summary> /// <param name="folder">The folder.</param> /// <returns>Whether the folder exists or not.</returns> public static bool Exists(AFolder folder) { Exceptions.NotNullException<AFolder>(folder, nameof(folder)); return Fenrir.FileSystem.FolderExists(folder.FullPath); }
/// <summary> /// Asynchronously moves the specified folder to the destination. /// </summary> /// <param name="source">The source.</param> /// <param name="destination">The destination.</param> /// <param name="overwriteFolder">if set to <c>true</c> overwrites the destination folder if it exists.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>A boolean task to move the folder. The boolean represents whether the folder was moved or not.</returns> public static async Task<bool> MoveAsync(AFolder source, AFolder destination, bool overwriteFolder = false, CancellationToken? cancellationToken = null) { await AwaitHelpers.CreateTaskScheduler(AwaitHelpers.CheckCancellationToken(cancellationToken)); return Move(source, destination, overwriteFolder); }
/// <summary> /// Asynchronously returns whether the folder exists or not. /// </summary> /// <param name="folder">The folder.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>A boolean task to check if the folder exists. The boolean represents whether the folder exists or not.</returns> public static async Task<bool> ExistsAsync(AFolder folder, CancellationToken? cancellationToken = null) { await AwaitHelpers.CreateTaskScheduler(AwaitHelpers.CheckCancellationToken(cancellationToken)); return Exists(folder); }
/// <summary> /// Internal function to get file system entries. /// </summary> /// <param name="folder">The folder.</param> /// <param name="searchPattern">The search pattern.</param> /// <param name="searchOption">The search option.</param> /// <returns>An enumerable representing the file system entries.</returns> private static IEnumerable<string> InternalGetFileSystemEntries(AFolder folder, string searchPattern, SearchOption searchOption) { var filesAndFolders = new List<string>(); if (searchOption != SearchOption.SubDirectoriesOnly) { var topFiles = folder.GetFileNames(); foreach (var file in topFiles) { if (Regex.IsMatch(file, searchPattern)) filesAndFolders.Add(file); } var topFolders = folder.GetFileNames(); foreach (var topFolder in topFolders) { if (Regex.IsMatch(topFolder, searchPattern)) filesAndFolders.Add(topFolder); } } var folders = folder.GetFolderNames(); foreach (var subFolder in folders) { if (Regex.IsMatch(subFolder, searchPattern)) filesAndFolders.Add(subFolder); if (searchOption != SearchOption.TopDirectoryOnly) { var subFiles = EnumerateFileSystemEntries(subFolder, searchPattern, SearchOption.AllDirectories); foreach (var file in subFiles) { if (Regex.IsMatch(file, searchPattern)) filesAndFolders.Add(file); } } } return filesAndFolders; }
/// <summary> /// Asynchronously gets the UTC creation time of the folder. /// </summary> /// <param name="folder">The folder.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>A DateTime task to get the creation time. The DateTime represents the creation time of the folder.</returns> public static async Task<DateTime> GetCreationTimeUtcAsync(AFolder folder, CancellationToken? cancellationToken = null) { await AwaitHelpers.CreateTaskScheduler(AwaitHelpers.CheckCancellationToken(cancellationToken)); return GetCreationTimeUtc(folder); }
/// <summary> /// Creates the folder. /// </summary> /// <param name="folder">The folder.</param> /// <returns>Whether the folder was created or not.</returns> public static bool CreateFolder(AFolder folder) { Exceptions.NotNullException<AFolder>(folder, nameof(folder)); try { return Fenrir.FileSystem.CreateFolder(folder.FullPath, FileCollisionOption.FailIfExists) != null; } catch { return false; } }
/// <summary> /// Returns an string list of all folders in the folder that match the search pattern and are within the search option choosen. /// </summary> /// <param name="folder">The folder.</param> /// <param name="searchPattern">The search pattern.</param> /// <param name="searchOption">The search option.</param> /// <returns>A string list representing the names of all folders in the folder.</returns> public static List<string> GetFoldersList(AFolder folder, string searchPattern = "*", SearchOption searchOption = SearchOption.AllDirectories) { Exceptions.NotNullException<AFolder>(folder, nameof(folder)); var entries = new List<string>(); foreach (var entry in InternalGetFolders(folder, searchPattern, searchOption)) entries.Add(entry); return entries; }
/// <summary> /// Internal function to get folders. /// </summary> /// <param name="folder">The folder.</param> /// <param name="searchPattern">The search pattern.</param> /// <param name="searchOption">The search option.</param> /// <returns>An enumerable representing the folders.</returns> private static IEnumerable<string> InternalGetFolders(AFolder folder, string searchPattern, SearchOption searchOption) { var folders = new List<string>(); var subFolders = folder.GetFolderNames(); foreach (var subFolder in subFolders) { if (Regex.IsMatch(subFolder, searchPattern)) folders.Add(subFolder); if (searchOption != SearchOption.TopDirectoryOnly) { var subFolderFolders = EnumerateFolders(subFolder, searchPattern, SearchOption.AllDirectories); foreach (var tmp in subFolderFolders) { if (Regex.IsMatch(tmp, searchPattern)) folders.Add(tmp); } } } return folders; }
/// <summary> /// Creates a folder asynchronously. /// </summary> /// <param name="directory">The directory.</param> /// <param name="name">The name.</param> /// <param name="collisionOption">The collision option. Defaults to FailIfExists.</param> /// <param name="cancellationToken">The cancellation token. Defaults to null.</param> /// <returns>An AFolder task to create a new folder. The AFolder represents the folder.</returns> public async Task<AFolder> CreateFolderAsync(AFolder directory, string name, FileCollisionOption collisionOption = FileCollisionOption.FailIfExists, CancellationToken? cancellationToken = null) { await AwaitHelpers.CreateTaskScheduler(AwaitHelpers.CheckCancellationToken(cancellationToken)); return CreateFolder(directory, name, collisionOption); }
/// <summary> /// Deletes the specified folder. /// </summary> /// <param name="folder">The folder.</param> /// <returns>Whether the folder was deleted or not.</returns> public static bool Delete(AFolder folder) { Exceptions.NotNullException<AFolder>(folder, nameof(folder)); return folder.Delete(); }
/// <summary> /// Generates a unique name for a folder asynchronously. /// </summary> /// <param name="directory">The directory.</param> /// <param name="name">The name.</param> /// <param name="iterations">The maximum iterations. Defaults to 99.</param> /// <param name="cancellationToken">The cancellation token. Defaults to null.</param> /// <returns> /// A string task to generate a unique folder name. The string represents a new unique name /// for the folder. /// </returns> public async Task<string> GenerateFolderUniqueNameAsync(AFolder directory, string name, int iterations = 99, CancellationToken? cancellationToken = null) { await AwaitHelpers.CreateTaskScheduler(AwaitHelpers.CheckCancellationToken(cancellationToken)); return GenerateFolderUniqueName(directory, name, iterations); }
/// <summary> /// Asynchronously returns an enumeration of all files and folders in the folder that match the search pattern and are within the search option choosen. /// </summary> /// <param name="folder">The folder.</param> /// <param name="searchPattern">The search pattern.</param> /// <param name="searchOption">The search option.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>An enumerable task to enumerate through the files and folders. The string enumerable represents the names of all files and folders in the folder.</returns> public static async Task<IEnumerable<string>> EnumerateFileSystemEntriesAsync(AFolder folder, string searchPattern = "*", SearchOption searchOption = SearchOption.AllDirectories, CancellationToken? cancellationToken = null) { await AwaitHelpers.CreateTaskScheduler(AwaitHelpers.CheckCancellationToken(cancellationToken)); return EnumerateFileSystemEntries(folder, searchPattern, searchOption); }
/// <summary> /// Gets a folder. /// </summary> /// <param name="directory">The directory.</param> /// <param name="folder">The folder.</param> /// <param name="openMode">The open mode. Defaults to Normal.</param> /// <returns>The folder.</returns> /// <exception cref="System.NotImplementedException"> /// Exception representing that this function is not implemented. /// </exception> public virtual AFolder OpenFolder(AFolder directory, string folder, OpenMode openMode) { throw new NotImplementedException(); }
/// <summary> /// Returns an enumeration of all folders in the folder that match the search pattern and are within the search option choosen. /// </summary> /// <param name="folder">The folder.</param> /// <param name="searchPattern">The search pattern.</param> /// <param name="searchOption">The search option.</param> /// <returns>A string enumerable representing the names of all folders in the folder.</returns> public static IEnumerable<string> EnumerateFolders(AFolder folder, string searchPattern = "*", SearchOption searchOption = SearchOption.AllDirectories) { Exceptions.NotNullException<AFolder>(folder, nameof(folder)); return InternalGetFolders(folder, searchPattern, searchOption); }
/// <summary> /// Creates a file. /// </summary> /// <param name="directory">The directory.</param> /// <param name="name">The name.</param> /// <param name="collisionOption">The collision option. Defaults to FailIfExists.</param> /// <returns>An AFile representing the file.</returns> /// <exception cref="System.NotImplementedException"> /// Exception representing that this function is not implemented. /// </exception> public virtual AFile CreateFile(AFolder directory, string name, FileCollisionOption collisionOption = FileCollisionOption.FailIfExists) { throw new NotImplementedException(); }
/// <summary> /// Creates a folder. /// </summary> /// <param name="directory">The directory.</param> /// <param name="name">The name.</param> /// <param name="collisionOption">The collision option. Defaults to FailIfExists.</param> /// <returns>An AFolder representing the folder.</returns> public override AFolder CreateFolder(AFolder directory, string name, FileCollisionOption collisionOption = FileCollisionOption.FailIfExists) { Exceptions.NotNullCheck<AFolder>(directory, nameof(directory)); Exceptions.NotNullOrEmptyCheck(name, nameof(name)); switch (collisionOption) { case FileCollisionOption.FailIfExists: if (Fenrir.FileSystem.FolderExists(System.IO.Path.Combine(directory.FullPath, name))) return null; break; case FileCollisionOption.GenerateUniqueName: name = Fenrir.FileSystem.GenerateFileUniqueName(directory.FullPath, name); break; } string folder = System.IO.Path.Combine(directory.FullPath, name); Directory.CreateDirectory(folder); return new FenrirFolder(folder); }