Esempio n. 1
0
        public async Task <Stream> OpenFileAsync(string path, FileMode mode, FileAccess access)
        {
            StorageFile file;

            switch (mode)
            {
            case FileMode.CreateNew:
                file = await _folder.CreateFileAsync(path, CreationCollisionOption.FailIfExists).AsTask().ConfigureAwait(false);

                break;

            case FileMode.Create:
            case FileMode.Truncate:                     // TODO See if ReplaceExisting already truncates
                file = await _folder.CreateFileAsync(path, CreationCollisionOption.ReplaceExisting).AsTask().ConfigureAwait(false);

                break;

            case FileMode.OpenOrCreate:
            case FileMode.Append:
                file = await _folder.CreateFileAsync(path, CreationCollisionOption.OpenIfExists).AsTask().ConfigureAwait(false);

                break;

            case FileMode.Open:
                file = await _folder.GetFileAsync(path);

                break;

            default:
                throw new ArgumentException("mode was an invalid FileMode", "mode");
            }

            switch (access)
            {
            case FileAccess.Read:
                return(await file.OpenStreamForReadAsync().ConfigureAwait(false));

            case FileAccess.Write:
                Stream stream = await file.OpenStreamForWriteAsync().ConfigureAwait(false);

                if (mode == FileMode.Append)
                {
                    stream.Position = stream.Length;
                }

                return(stream);

            case FileAccess.ReadWrite:
                IRandomAccessStream randStream = await file.OpenAsync(FileAccessMode.ReadWrite).AsTask().ConfigureAwait(false);

                return(randStream.AsStream());

            default:
                throw new ArgumentException("access was an invalid FileAccess", "access");
            }
        }
Esempio n. 2
0
 public Task <Stream> OpenFileAsync(string path, FileMode mode, FileAccess access, FileShare share)
 {
     return(OpenFileAsync(path, mode, access));
 }