Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
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);
            }
        }
Ejemplo n.º 3
0
        public async Task Delete()
        {
            if (await Exists())
            {
                var file = await UWPFile.GetFileFromPathAsync(FullPath);

                await file.DeleteAsync();
            }
        }
Ejemplo n.º 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);
            }
        }
Ejemplo n.º 5
0
        public static async Task <StorageFile> GetFileFromPath(string path)
        {
            var file = await UWPFile.GetFileFromPathAsync(path);

            return(new StorageFile
            {
                Attributes = (FileAttributes)((int)file.Attributes),
                DateCreated = file.DateCreated.DateTime,
                DisplayName = file.DisplayName,
                DisplayType = file.DisplayType,
                FullPath = file.Path,
                Name = file.Name
            });
        }
Ejemplo n.º 6
0
        public async Task <Stream> Open(FileAccessMode mode)
        {
            if (await Exists())
            {
                var file = await UWPFile.GetFileFromPathAsync(FullPath);

                switch (mode)
                {
                case FileAccessMode.Read:
                    return(await file.OpenStreamForReadAsync());

                case FileAccessMode.Write:
                    return(await file.OpenStreamForWriteAsync());
                }
                return(null);
            }
            else
            {
                throw new FileNotFoundException("El archivo que tratas de copiar no existe, path: " + FullPath + ", name: " + Name);
            }
        }
Ejemplo n.º 7
0
        private async Task <bool> Exists()
        {
            var file = await UWPFile.GetFileFromPathAsync(FullPath);

            return(file != null);
        }