示例#1
0
 public override async Task <IReadOnlyList <IStorageItem> > GetItemsAsync(CancellationToken ct)
 {
     return(await Task.Run(() =>
     {
         var items = _directoryDocument.ListFiles();
         var folders = items.Where(i => i.IsDirectory).Select(i => GetFromSafDocument(i));
         var files = items.Where(i => i.IsFile).Select(i => StorageFile.GetFromSafDocument(i));
         return Task.FromResult((IReadOnlyList <IStorageItem>)folders.OfType <IStorageItem>().Union(files).ToArray());
     }, ct));
 }
示例#2
0
            public override async Task <StorageFile> CreateFileAsync(string desiredName, Windows.Storage.CreationCollisionOption options, CancellationToken cancellationToken)
            {
                return(await Task.Run(async() =>
                {
                    var actualName = desiredName;

                    var existingItem = await TryGetItemAsync(desiredName, cancellationToken);
                    switch (options)
                    {
                    case CreationCollisionOption.ReplaceExisting:
                        if (existingItem is StorageFolder)
                        {
                            throw new UnauthorizedAccessException("There is already a folder with the same name.");
                        }

                        if (existingItem is StorageFile)
                        {
                            // Delete existing file
                            await existingItem.DeleteAsync();
                        }
                        break;

                    case CreationCollisionOption.FailIfExists:
                        if (existingItem != null)
                        {
                            throw new UnauthorizedAccessException("There is already an item with the same name.");
                        }
                        break;

                    case CreationCollisionOption.OpenIfExists:
                        if (existingItem is StorageFile existingFile)
                        {
                            return existingFile;
                        }

                        if (existingItem is StorageFolder)
                        {
                            throw new UnauthorizedAccessException("There is already a file with the same name.");
                        }
                        break;

                    case CreationCollisionOption.GenerateUniqueName:
                        actualName = await FindAvailableNumberedFileNameAsync(desiredName);
                        break;

                    default:
                        throw new ArgumentOutOfRangeException(nameof(options));
                    }

                    var extension = IOPath.GetExtension(actualName);
                    var mimeType = MimeTypeService.GetFromExtension(extension);
                    var file = _directoryDocument.CreateFile("", actualName);
                    return StorageFile.GetFromSafDocument(file);
                }, cancellationToken));
            }
示例#3
0
 public override async Task <IReadOnlyList <StorageFile> > GetFilesAsync(CancellationToken ct)
 {
     return(await Task.Run(() =>
     {
         var contents = _directoryDocument
                        .ListFiles()
                        .Where(f => f.IsFile)
                        .Select(d => StorageFile.GetFromSafDocument(d))
                        .ToArray();
         return Task.FromResult((IReadOnlyList <StorageFile>)contents);
     }, ct));
 }
示例#4
0
            public override async Task <IStorageItem> GetItemAsync(string name, CancellationToken token)
            {
                return(await Task.Run <IStorageItem>(() =>
                {
                    var item = _directoryDocument.FindFile(name);
                    if (item == null || (!item.IsDirectory && !item.IsFile))
                    {
                        throw new FileNotFoundException("Not found");
                    }

                    if (item.IsDirectory)
                    {
                        return GetFromSafDocument(item);
                    }

                    return StorageFile.GetFromSafDocument(item);
                }, token));
            }
示例#5
0
            public override async Task <StorageFile> GetFileAsync(string name, CancellationToken token)
            {
                return(await Task.Run(() =>
                {
                    var item = _directoryDocument.FindFile(name);
                    if (item == null)
                    {
                        throw new FileNotFoundException("There is no file with this name.");
                    }

                    if (!item.IsFile)
                    {
                        throw new ArgumentException("The item with given name is a folder.", nameof(name));
                    }

                    return StorageFile.GetFromSafDocument(item);
                }, token));
            }
示例#6
0
            public override async Task <IStorageItem?> TryGetItemAsync(string name, CancellationToken token)
            {
                return(await Task.Run <IStorageItem?>(() =>
                {
                    var item = _directoryDocument.FindFile(name);
                    if (item == null)
                    {
                        return null;
                    }

                    if (item.IsDirectory)
                    {
                        return GetFromSafDocument(item);
                    }

                    if (item.IsFile)
                    {
                        return StorageFile.GetFromSafDocument(item);
                    }

                    return null;
                }, token));
            }