/// <summary>
        /// Gets the folder that has the specified absolute path in the file system.
        /// </summary>
        /// <param name="path">
        /// The absolute path in the file system (not the Uri) of the folder to get.
        /// </param>
        /// <returns>
        /// When this method completes successfully, it returns an IAppFolder that represents the specified folder.
        /// </returns>
        public static async Task <IStorageFolder> GetFolderFromPathAsync(string path)
        {
            await TaskSchedulerAwaiter.NewTaskSchedulerAwaiter();

            IStorageFolder resultParentFolder;

            if (Directory.Exists(path))
            {
                var directoryInfo = new DirectoryInfo(path);
                if (directoryInfo.Parent != null && directoryInfo.Parent.Exists)
                {
                    resultParentFolder = await GetFolderFromPathAsync(directoryInfo.Parent.FullName);
                }
                else
                {
                    resultParentFolder = null;
                }
            }
            else
            {
                return(null);
            }

            var resultFolder = new StorageFolder(resultParentFolder, path);

            return(resultFolder);
        }
Esempio n. 2
0
        /// <inheritdoc />
        public async Task MoveAndReplaceAsync(IStorageFile fileToReplace)
        {
            if (!this.Exists)
            {
                throw new StorageItemNotFoundException(this.Name, "Cannot move a file that does not exist.");
            }

            if (fileToReplace == null)
            {
                throw new ArgumentNullException(nameof(fileToReplace));
            }

            if (!fileToReplace.Exists)
            {
                throw new StorageItemNotFoundException(
                          fileToReplace.Name,
                          "Cannot move to and replace a file that does not exist.");
            }

            await TaskSchedulerAwaiter.NewTaskSchedulerAwaiter();

            var newPath = fileToReplace.Path;

            File.Delete(newPath);
            File.Move(this.Path, newPath);

            this.Path   = newPath;
            this.Parent = fileToReplace.Parent;
        }
        /// <inheritdoc />
        public async Task <IStorageFolder> GetFolderAsync(string name, bool createIfNotExists)
        {
            if (!this.Exists)
            {
                throw new StorageItemNotFoundException(
                          this.Name,
                          "Cannot get a folder from a folder that does not exist.");
            }

            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            await TaskSchedulerAwaiter.NewTaskSchedulerAwaiter();

            var folderPath = System.IO.Path.Combine(this.Path, name);

            if (!Directory.Exists(folderPath))
            {
                if (createIfNotExists)
                {
                    return(await this.CreateFolderAsync(name));
                }

                throw new StorageItemNotFoundException(name, "The folder could not be found in the folder.");
            }

            return(new StorageFolder(this, folderPath));
        }
Esempio n. 4
0
        /// <summary>
        /// Gets an IStorageFile object to represent the file at the specified path.
        /// </summary>
        /// <param name="path">
        /// The path of the file to get an IStorageFile to represent.
        /// </param>
        /// <returns>
        /// When this method completes, it returns the file as an IStorageFile.
        /// </returns>
        public static async Task <IStorageFile> GetFileFromPathAsync(string path)
        {
            await TaskSchedulerAwaiter.NewTaskSchedulerAwaiter();

            IStorageFolder resultFileParentFolder;

            if (File.Exists(path))
            {
                var fileInfo = new FileInfo(path);
                if (fileInfo.Directory != null && fileInfo.Directory.Exists)
                {
                    resultFileParentFolder = await StorageFolder.GetFolderFromPathAsync(fileInfo.Directory.FullName);
                }
                else
                {
                    resultFileParentFolder = null;
                }
            }
            else
            {
                return(null);
            }

            var resultFile = new StorageFile(resultFileParentFolder, path);

            return(resultFile);
        }
Esempio n. 5
0
        /// <inheritdoc />
        public async Task RenameAsync(string desiredName, NameCollisionOption option)
        {
            if (!this.Exists)
            {
                throw new StorageItemNotFoundException(this.Name, "Cannot rename a file that does not exist.");
            }

            if (string.IsNullOrWhiteSpace(desiredName))
            {
                throw new ArgumentNullException(nameof(desiredName));
            }

            if (desiredName.Equals(this.Name, StringComparison.CurrentCultureIgnoreCase))
            {
                throw new ArgumentException("The desired new name is the same as the current name.");
            }

            await TaskSchedulerAwaiter.NewTaskSchedulerAwaiter();

            var fileInfo = new FileInfo(this.Path);

            if (fileInfo.Directory == null)
            {
                throw new InvalidOperationException("This file cannot be renamed.");
            }

            string newPath = System.IO.Path.Combine(fileInfo.Directory.FullName, desiredName);

            switch (option)
            {
            case NameCollisionOption.GenerateUniqueName:
                newPath = System.IO.Path.Combine(
                    fileInfo.Directory.FullName,
                    string.Format("{0}-{1}", desiredName, Guid.NewGuid()));
                fileInfo.MoveTo(newPath);
                break;

            case NameCollisionOption.ReplaceExisting:
                if (File.Exists(newPath))
                {
                    File.Delete(newPath);
                }

                fileInfo.MoveTo(newPath);
                break;

            default:
                if (File.Exists(newPath))
                {
                    throw new StorageItemCreationException(
                              desiredName,
                              "A file with the same name already exists.");
                }

                fileInfo.MoveTo(newPath);
                break;
            }

            this.Path = newPath;
        }
Esempio n. 6
0
        /// <inheritdoc />
        public async Task DeleteAsync()
        {
            if (!this.Exists)
            {
                throw new StorageItemNotFoundException(this.Name, "Cannot delete a file that does not exist.");
            }

            await TaskSchedulerAwaiter.NewTaskSchedulerAwaiter();

            File.Delete(this.Path);
        }
        /// <inheritdoc />
        public async Task <IStorageFolder> CreateFolderAsync(string desiredName, CreationCollisionOption options)
        {
            if (!this.Exists)
            {
                throw new StorageItemNotFoundException(
                          this.Name,
                          "Cannot create a folder in a folder that does not exist.");
            }

            if (string.IsNullOrWhiteSpace(desiredName))
            {
                throw new ArgumentNullException(nameof(desiredName));
            }

            await TaskSchedulerAwaiter.NewTaskSchedulerAwaiter();

            var folderPath = System.IO.Path.Combine(this.Path, desiredName);

            if (Directory.Exists(folderPath))
            {
                switch (options)
                {
                case CreationCollisionOption.GenerateUniqueName:
                    desiredName = string.Format("{0}-{1}", desiredName, Guid.NewGuid());
                    folderPath  = System.IO.Path.Combine(this.Path, desiredName);
                    CreateFolder(folderPath);
                    break;

                case CreationCollisionOption.ReplaceExisting:
                    if (Directory.Exists(folderPath))
                    {
                        Directory.Delete(folderPath);
                    }
                    CreateFolder(folderPath);
                    break;

                case CreationCollisionOption.FailIfExists:
                    if (Directory.Exists(folderPath))
                    {
                        throw new StorageItemCreationException(
                                  desiredName,
                                  "A folder with the same name already exists.");
                    }
                    CreateFolder(folderPath);
                    break;
                }
            }
            else
            {
                CreateFolder(folderPath);
            }

            return(new StorageFolder(this, folderPath));
        }
        /// <inheritdoc />
        public async Task <IReadOnlyList <IStorageFolder> > GetFoldersAsync()
        {
            if (!this.Exists)
            {
                throw new StorageItemNotFoundException(
                          this.Name,
                          "Cannot get folders from a folder that does not exist.");
            }

            await TaskSchedulerAwaiter.NewTaskSchedulerAwaiter();

            return(Directory.GetDirectories(this.Path).Select(folderPath => new StorageFolder(this, folderPath)).ToList());
        }
        /// <inheritdoc />
        public async Task <IDictionary <string, object> > GetPropertiesAsync()
        {
            if (!this.Exists)
            {
                throw new StorageItemNotFoundException(
                          this.Name,
                          "Cannot get properties for a folder that does not exist.");
            }

            await TaskSchedulerAwaiter.NewTaskSchedulerAwaiter();

            // ToDo, current not implemented.
            return(new Dictionary <string, object>());
        }
Esempio n. 10
0
        /// <inheritdoc />
        public async Task <IStorageItem> GetItemAsync(string name)
        {
            if (!this.Exists)
            {
                throw new StorageItemNotFoundException(
                          this.Name,
                          "Cannot get an item from a folder that does not exist.");
            }

            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            await TaskSchedulerAwaiter.NewTaskSchedulerAwaiter();

            IStorageItem storageItem = null;

            try
            {
                storageItem = await this.GetFileAsync(name);
            }
            catch (Exception ex)
            {
#if DEBUG
                System.Diagnostics.Debug.WriteLine(ex.Message);
#endif
            }

            try
            {
                storageItem = await this.GetFolderAsync(name);
            }
            catch (Exception ex)
            {
#if DEBUG
                System.Diagnostics.Debug.WriteLine(ex.Message);
#endif
            }

            if (storageItem == null || !storageItem.Exists)
            {
                throw new StorageItemNotFoundException(name, "The item could not be found in the folder.");
            }

            return(storageItem);
        }
Esempio n. 11
0
        /// <inheritdoc />
        public async Task <IDictionary <string, object> > GetPropertiesAsync()
        {
            if (!this.Exists)
            {
                throw new StorageItemNotFoundException(
                          this.Name,
                          "Cannot get properties for a file that does not exist.");
            }

            await TaskSchedulerAwaiter.NewTaskSchedulerAwaiter();

            var props = new Dictionary <string, object>();

            // ToDo; find a library or implement metadata extraction from file.

            return(props);
        }
Esempio n. 12
0
        /// <inheritdoc />
        public async Task <Stream> OpenAsync(FileAccessMode accessMode)
        {
            if (!this.Exists)
            {
                throw new StorageItemNotFoundException(this.Name, "Cannot open a file that does not exist.");
            }

            await TaskSchedulerAwaiter.NewTaskSchedulerAwaiter();

            switch (accessMode)
            {
            case FileAccessMode.Read:
                return(File.OpenRead(this.Path));

            case FileAccessMode.ReadWrite:
                return(File.Open(this.Path, FileMode.Open, FileAccess.ReadWrite));

            default:
                throw new StorageFileIOException(this.Name, "The file could not be opened.");
            }
        }
Esempio n. 13
0
        /// <inheritdoc />
        public async Task <IReadOnlyList <IStorageItem> > GetItemsAsync()
        {
            if (!this.Exists)
            {
                throw new StorageItemNotFoundException(
                          this.Name,
                          "Cannot get items from a folder that does not exist.");
            }

            await TaskSchedulerAwaiter.NewTaskSchedulerAwaiter();

            var files = await this.GetFilesAsync();

            var folders = await this.GetFoldersAsync();

            var items = new List <IStorageItem>();

            items.AddRange(files);
            items.AddRange(folders);

            return(items);
        }
Esempio n. 14
0
        /// <inheritdoc />
        public async Task CopyAndReplaceAsync(IStorageFile fileToReplace)
        {
            if (!this.Exists)
            {
                throw new StorageItemNotFoundException(this.Name, "Cannot copy a file that does not exist.");
            }

            if (fileToReplace == null)
            {
                throw new ArgumentNullException(nameof(fileToReplace));
            }

            if (!fileToReplace.Exists)
            {
                throw new StorageItemNotFoundException(
                          fileToReplace.Name,
                          "Cannot copy to and replace a file that does not exist.");
            }

            await TaskSchedulerAwaiter.NewTaskSchedulerAwaiter();

            File.Copy(this.Path, fileToReplace.Path, true);
        }
Esempio n. 15
0
        /// <inheritdoc />
        public async Task MoveAsync(
            IStorageFolder destinationFolder,
            string desiredNewName,
            NameCollisionOption option)
        {
            if (!this.Exists)
            {
                throw new StorageItemNotFoundException(this.Name, "Cannot move a file that does not exist.");
            }

            if (destinationFolder == null)
            {
                throw new ArgumentNullException(nameof(destinationFolder));
            }

            if (!destinationFolder.Exists)
            {
                throw new StorageItemNotFoundException(
                          destinationFolder.Name,
                          "Cannot move a file to a folder that does not exist.");
            }

            if (string.IsNullOrWhiteSpace(desiredNewName))
            {
                throw new ArgumentNullException(nameof(desiredNewName));
            }

            await TaskSchedulerAwaiter.NewTaskSchedulerAwaiter();

            string newPath = System.IO.Path.Combine(destinationFolder.Path, desiredNewName);

            switch (option)
            {
            case NameCollisionOption.GenerateUniqueName:
                newPath = System.IO.Path.Combine(
                    destinationFolder.Path,
                    string.Format("{0}-{1}", Guid.NewGuid(), desiredNewName));

                File.Move(this.Path, newPath);
                break;

            case NameCollisionOption.ReplaceExisting:
                if (File.Exists(newPath))
                {
                    File.Delete(newPath);
                }

                File.Move(this.Path, newPath);
                break;

            default:
                if (File.Exists(newPath))
                {
                    throw new StorageItemCreationException(
                              desiredNewName,
                              "A file with the same name already exists.");
                }

                File.Move(this.Path, newPath);
                break;
            }

            this.Path = newPath;
        }
 public AwaiterWrapper(TaskSchedulerAwaiter awaiter)
 {
     isCompleted = () => awaiter.IsCompleted;
     onCompleted = c => awaiter.OnCompleted(c);
     getResult   = () => awaiter.GetResult();
 }
Esempio n. 17
0
 public AwaiterWrapper(TaskSchedulerAwaiter awaiter)
 {
     isCompleted = () => awaiter.IsCompleted;
     onCompleted = c => awaiter.OnCompleted(c);
     getResult = () => awaiter.GetResult();
 }