/// <summary> /// Gets the directory specified by the directoryName, can be immediate directory or nested subdirectory /// </summary> /// <param name="pathToDirectory">Lenovo OR Lenovo\Information\Test</param> /// <returns></returns> public Task <IDirectory> GetDirectoryAsync(string pathToDirectory) { AssertDirectoryExists(); return(Task.Factory.StartNew(() => { IDirectory directory = null; if (!String.IsNullOrWhiteSpace(pathToDirectory)) { // Get full path to the desired directory string directoryPath = Path.Combine(_systemPathMapper.GetUserContextFolder(this.FullPath), pathToDirectory); if (Directory.Exists(directoryPath)) { // Create a new directory info object for the desired directory var directoryInfo = new DirectoryInfo(directoryPath); //if (directoryInfo != null) //{ // Create new win directory object using directoryInfo directory = new SystemContextDirectory(directoryInfo); //} } } return directory; })); }
/// <summary> /// Creates a directory using the given path and creation option /// </summary> /// <param name="directoryName"></param> /// <param name="collisionOption"></param> /// <returns></returns> public Task <IDirectory> CreateDirectoryAsync(string directoryPath, CreationOption collisionOption) { AssertDirectoryExists(); return(Task.Factory.StartNew(() => { IDirectory createdDirectory = null; string expandedPath = _systemPathMapper.GetUserContextFolder(directoryPath); // Check to see if the path is a relative or full path if (!Path.IsPathRooted(expandedPath)) { // If it's a relative path, then create the new full path using this directory's full path expandedPath = Path.Combine(this.FullPath, directoryPath); } // Verify the directory exists if (Directory.Exists(expandedPath)) { switch (collisionOption) { case CreationOption.OpenIfExists: // Just open the directory if it already exists createdDirectory = new SystemContextDirectory(new DirectoryInfo(expandedPath)); break; case CreationOption.ReplaceExisting: // Replace the existing directory with a new one, must first delete it (recursively delete all sub directories as well) Directory.Delete(expandedPath, true); createdDirectory = new SystemContextDirectory(Directory.CreateDirectory(expandedPath)); break; case CreationOption.ThrowIfExists: throw new Exceptions.DirectoryAlreadyExistsException("The directory to be created already exists!"); default: goto case CreationOption.ReplaceExisting; } } else { // Directory does not already exist, just go ahead and create it createdDirectory = new SystemContextDirectory(Directory.CreateDirectory(expandedPath)); } return createdDirectory; })); }