예제 #1
0
            public override async Task <StorageFile> CreateFileAsync(string desiredName, CreationCollisionOption options, CancellationToken cancellationToken)
            {
                using var _ = _nsUrl.BeginSecurityScopedAccess();
                var path       = IOPath.Combine(Path, desiredName);
                var actualName = desiredName;

                switch (options)
                {
                case CreationCollisionOption.FailIfExists:
                    if (Directory.Exists(path) || File.Exists(path))
                    {
                        throw new UnauthorizedAccessException("There is already an item with the same name.");
                    }
                    break;

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

                    break;

                case CreationCollisionOption.OpenIfExists:
                    if (Directory.Exists(path))
                    {
                        throw new UnauthorizedAccessException("There is already a folder with the same name.");
                    }
                    break;

                case CreationCollisionOption.ReplaceExisting:
                    if (Directory.Exists(path))
                    {
                        throw new UnauthorizedAccessException("There is already a folder with the same name.");
                    }

                    if (File.Exists(path))
                    {
                        File.Delete(path);
                    }
                    break;

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

                var actualPath = IOPath.Combine(Path, actualName);

                if (!File.Exists(actualPath))
                {
                    File.Create(actualPath).Close();
                }

                return(StorageFile.GetFromSecurityScopedUrl(_nsUrl.Append(actualName, false), Owner));
            }
예제 #2
0
            public override Task <IReadOnlyList <StorageFile> > GetFilesAsync(CancellationToken ct)
            {
                using var _ = _nsUrl.BeginSecurityScopedAccess();
                var items = new List <StorageFile>();

                foreach (var file in Directory.EnumerateFiles(Path))
                {
                    var fileUrl = _nsUrl.Append(IOPath.GetFileName(file), false);
                    items.Add(StorageFile.GetFromSecurityScopedUrl(fileUrl, Owner));
                }

                return(Task.FromResult <IReadOnlyList <StorageFile> >(items.AsReadOnly()));
            }
예제 #3
0
            public override Task <StorageFile> GetFileAsync(string name, CancellationToken token)
            {
                using var _ = _nsUrl.BeginSecurityScopedAccess();

                var filePath = IOPath.Combine(Path, name);

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

                if (!File.Exists(filePath))
                {
                    throw new FileNotFoundException("There is no file with this name.");
                }

                return(Task.FromResult(StorageFile.GetFromSecurityScopedUrl(_nsUrl.Append(name, false), Owner)));
            }
예제 #4
0
            public override Task <IStorageItem?> TryGetItemAsync(string name, CancellationToken token)
            {
                using var _ = _nsUrl.BeginSecurityScopedAccess();
                var itemUrl = _nsUrl.Append(name, false);

                if (itemUrl.CheckPromisedItemIsReachable(out var _))
                {
                    var document = new UIDocument(itemUrl);
                    if (document.FileType == UTType.Folder)
                    {
                        return(Task.FromResult <IStorageItem?>(GetFromSecurityScopedUrl(itemUrl, Owner)));
                    }
                    else
                    {
                        return(Task.FromResult <IStorageItem?>(StorageFile.GetFromSecurityScopedUrl(itemUrl, Owner)));
                    }
                }

                return(Task.FromResult <IStorageItem?>(null));
            }