Пример #1
0
        internal static async Task <WinStorageFolder> GetFolderAsync(
            StoragePath fullPath,
            CancellationToken cancellationToken
            )
        {
            try
            {
                return(await WinStorageFolder.GetFolderFromPathAsync(fullPath).AsAwaitable(cancellationToken));
            }
            catch (FileNotFoundException ex)
            {
                throw new DirectoryNotFoundException(message: null, innerException: ex);
            }
            catch (ArgumentException ex)
            {
                // An ArgumentException might indicate that a conflicting file exists at this location.
                // Try to get a file. If that succeedes, we can be certain and throw the IOException
                // which is required by specification.
                try
                {
                    await WinStorageFile.GetFileFromPathAsync(fullPath).AsAwaitable(cancellationToken);
                }
                catch
                {
                    // Getting the file failed too. Rethrow the ArgumentException from before.
                    ExceptionDispatchInfo.Throw(ex);
                }

                // At this point, getting the file succeeded. We must manually throw to fulfill the specification.
                throw new IOException(ExceptionStrings.StorageFolder.ConflictingFileExistsAtFolderLocation(), ex);
            }
        }
Пример #2
0
        public async Task <IStorageFile> CopyTo(IStorageFolder folderdestination, string filenamewithextension, NameCollisionOption option)
        {
            if (await Exists())
            {
                var file = await UWPFile.GetFileFromPathAsync(FullPath);

                var folder = await UWPFolder.GetFolderFromPathAsync(folderdestination.FullPath);

                switch (option)
                {
                case NameCollisionOption.GenerateUniqueName:
                    await file.CopyAsync(folder, filenamewithextension, Windows.Storage.NameCollisionOption.GenerateUniqueName);

                    break;

                case NameCollisionOption.ReplaceExisting:
                    await file.CopyAsync(folder, filenamewithextension, Windows.Storage.NameCollisionOption.ReplaceExisting);

                    break;

                case NameCollisionOption.FailIfExists:
                    await file.CopyAsync(folder, filenamewithextension, Windows.Storage.NameCollisionOption.FailIfExists);

                    break;
                }
            }
            return(null);
        }
Пример #3
0
        public async Task <IStorageFile> GetFile(string filenamewithextension)
        {
            var winfolder = await UWPFolder.GetFolderFromPathAsync(FullPath);

            var file = await winfolder.GetFileAsync(filenamewithextension);

            return(await StorageFile.GetFileFromPath(file.Path));
        }
Пример #4
0
        internal static async Task <WinStorageFile> GetFileAsync(
            StoragePath fullPath,
            CancellationToken cancellationToken
            )
        {
            try
            {
                return(await WinStorageFile.GetFileFromPathAsync(fullPath).AsAwaitable(cancellationToken));
            }
            catch (FileNotFoundException ex)
            {
                // FileNotFoundException might be thrown if the parent directory does not exist.
                // Specification requires DirectoryNotFoundException in such cases.

                // Some folder operations might call this method though. In such cases, we might not
                // have a parent path. In that case, just go with the FileNotFoundException.
                if (fullPath.Parent is null)
                {
                    throw;
                }

                try
                {
                    await WinStorageFolder.GetFolderFromPathAsync(fullPath.Parent).AsAwaitable(cancellationToken);
                }
                catch
                {
                    // Getting the parent folder failed.
                    throw new DirectoryNotFoundException(ExceptionStrings.StorageFile.ParentFolderDoesNotExist(), ex);
                }

                // At this point we know that the parent folder exists, but the file doesn't.
                // We can go with the FileNotFoundException.
                throw;
            }
            catch (ArgumentException ex)
            {
                // An ArgumentException might indicate that a conflicting folder exists at this location.
                // Try to get a folder. If that succeedes, we can be certain and throw the IOException
                // which is required by specification.
                try
                {
                    await WinStorageFolder.GetFolderFromPathAsync(fullPath).AsAwaitable(cancellationToken);
                }
                catch
                {
                    // Getting the folder failed too. Rethrow the ArgumentException from before.
                    ExceptionDispatchInfo.Throw(ex);
                }

                // At this point, getting the folder succeeded. We must manually throw to fulfill the specification.
                throw new IOException(ExceptionStrings.StorageFile.ConflictingFolderExistsAtFileLocation(), ex);
            }
        }
Пример #5
0
        public async Task <List <IStorageFile> > GetFiles()
        {
            List <IStorageFile> files = new List <IStorageFile>();
            var winfolder             = await UWPFolder.GetFolderFromPathAsync(FullPath);

            var winfiles = await winfolder.GetFilesAsync();

            foreach (var file in winfiles)
            {
                files.Add(new StorageFile
                {
                    Attributes  = FileAttributes.Normal,
                    DisplayName = file.DisplayName,
                    DisplayType = file.DisplayType,
                    FullPath    = file.Path,
                    Name        = file.Name,
                    DateCreated = file.DateCreated.DateTime
                });
            }
            return(files);
        }
Пример #6
0
        private static async Task <UWPFolder> GetUWPFolder(string path)
        {
            var folder = await UWPFolder.GetFolderFromPathAsync(path);

            return(folder);
        }