示例#1
0
        private async Task <Result <StorageFile> > ToNativeAsync(File file)
        {
            StorageFile storageFile;

            try
            {
                storageFile = await StorageFile.GetFileFromPathAsync(file.Path).AsTask().ConfigureAwait(false);
            }
            catch (FileNotFoundException e)
            {
                // The file does not exist
                Logger.Info(e);
                return(Result.Fail <StorageFile>(new FileNotFoundError("The file does not exist").CausedBy(e)));
            }
            catch (UnauthorizedAccessException e)
            {
                // Insufficient permission to access this folder.
                Logger.Info(e);
                return(Result.Fail <StorageFile>(new UnauthorizedAccessError("Insufficient permission to access this file.").CausedBy(e)));
            }
            catch (ArgumentException e)
            {
                // The path cannot be a relative path or a Uri.
                Logger.Info(e);
                return(Result.Fail <StorageFile>(new ArgumentError("The path cannot be a relative path or a Uri.").CausedBy(e)));
            }

            return(Result.Ok(storageFile));
        }
示例#2
0
        public static async Task <Result <StorageFile> > ToNative(this File file)
        {
            Guard.IsNotNull(file, nameof(file));

            StorageFile nativeFile;

            try
            {
                nativeFile = await StorageFile.GetFileFromPathAsync(file.Path).AsTask().ConfigureAwait(false);
            }
            catch (FileNotFoundException e)
            {
                // TODO include error information
                return(Result.Fail(new FileNotFoundError().CausedBy(e)));
            }
            catch (UnauthorizedAccessException e)
            {
                // TODO include error information
                return(Result.Fail(new UnauthorizedAccessError().CausedBy(e)));
            }
            catch (ArgumentException e)
            {
                // TODO include error information
                return(Result.Fail(new ArgumentError().CausedBy(e)));
            }

            return(Result.Ok <StorageFile>(nativeFile));
        }
示例#3
0
        private async Task <StorageFile> ToNative(File file)
        {
            var path = file.Path.Replace('\\', '/');

            var storageFile = await StorageApplicationPermissions.FutureAccessList.GetFileAsync(path);

            return(storageFile);
        }
示例#4
0
        public async Task <File> WriteTextAsync(File file, string content)
        {
            var storageFile = await StorageFile.GetFileFromPathAsync(file.Path).AsTask().ConfigureAwait(false);

            await FileIO.WriteTextAsync(storageFile, content);

            return(file);
        }
示例#5
0
        public async Task <string> ReadTextAsync(File file)
        {
            var result = await file.ToNative().ConfigureAwait(false);

            if (result.IsFailed)
            {
                // TODO
                throw new Exception("Exception");
            }

            var nativeFile = result.Value;

            return(await FileIO.ReadTextAsync(nativeFile).AsTask().ConfigureAwait(false));
        }
示例#6
0
        public async Task WriteTextAsync(File file, string content)
        {
            var result = await file.ToNative().ConfigureAwait(false);

            if (result.IsFailed)
            {
                // TODO
                throw new Exception("Exception");
            }

            var nativeFile = result.Value;

            await FileIO.WriteTextAsync(nativeFile, content).AsTask().ConfigureAwait(false);
        }
示例#7
0
        public async Task <Result <File> > CopyAsync(File source, Folder target)
        {
            Guard.ArgNotNull(source, nameof(source));
            Guard.ArgNotNull(target, nameof(target));

            var targetFolderTask = ToNativeAsync(target);
            var sourceFileTask   = ToNativeAsync(source);

            await Task.WhenAll(targetFolderTask, sourceFileTask).ConfigureAwait(false);

            var result2 = Result.Merge(await targetFolderTask, await sourceFileTask);

            if (result2.IsFailed)
            {
                return(result2.ToResult <File>());
            }

            var targetStorageFolder2 = (await targetFolderTask).Value;
            var sourceStorageFile2   = (await sourceFileTask).Value;

            var targetStorageFile = await sourceStorageFile2.CopyAsync(targetStorageFolder2).AsTask().ConfigureAwait(false);

            return(Result.Ok(new File(targetStorageFile.Path)));
        }
示例#8
0
        private Task <StorageFile> ToNative(File file)
        {
            var storageFile = StorageFile.GetFileFromPathAsync(file.Path).AsTask();

            return(storageFile);
        }
示例#9
0
        public async Task <string> ReadTextAsync(File file)
        {
            var storageFile = await ToNative(file);

            return(await FileIO.ReadTextAsync(storageFile));
        }