protected override Task DispatchAsyncCore(SetupContext context, CancellationToken cancellationToken)
            {
                FileInfo file = context.ResolveFile(this.File);

                file.Directory.Create();
                return(AsyncFile.WriteAllBytesAsync(file.FullName, this.Data.ToArray()));
            }
Exemplo n.º 2
0
            public async Task CancellationToken_ExceptionThrown()
            {
                var bytes  = new byte[100000];
                var random = new Random();

                random.NextBytes(bytes);

                var path = Path.Combine(writeAllBytesTestFolder, nameof(CancellationToken_ExceptionThrown));

                Directory.CreateDirectory(writeAllBytesTestFolder);

                var tokenSource = new CancellationTokenSource();

                tokenSource.Cancel();
                Assert.ThrowsAsync <TaskCanceledException>(
                    async() => await AsyncFile.WriteAllBytesAsync(path, bytes, tokenSource.Token).ConfigureAwait(false));
            }
Exemplo n.º 3
0
            public async Task Default_BytesWritten()
            {
                var bytes  = new byte[10000];
                var random = new Random();

                random.NextBytes(bytes);

                var path = Path.Combine(writeAllBytesTestFolder, nameof(Default_BytesWritten));

                Directory.CreateDirectory(writeAllBytesTestFolder);

                await AsyncFile.WriteAllBytesAsync(path, bytes).ConfigureAwait(false);

                var result = File.ReadAllBytes(path);

                CollectionAssert.AreEqual(bytes, result);
            }
Exemplo n.º 4
0
        /// <summary>
        /// Save byte array by absolue path.
        /// </summary>
        /// <param name="path">Absolute path.</param>
        /// <param name="bytes">Byte array to save.</param>
        /// <param name="createDirIfNotExists">Flag indicates to create destination directory if not exists.</param>
        /// <param name="overrideFileIfExists">Flag indicates to override destination file if exists.</param>
        /// <param name="cancellationToken">Cancellation token.</param>
        /// <returns>Absolute file path.</returns>
        public static async Task <string> SaveFileAsync(string path,
                                                        byte[] bytes,
                                                        bool createDirIfNotExists           = true,
                                                        bool overrideFileIfExists           = false,
                                                        CancellationToken cancellationToken = default(CancellationToken))
        {
            if (string.IsNullOrWhiteSpace(path))
            {
                throw new ArgumentNullException(nameof(path));
            }
            if (bytes == null)
            {
                throw new ArgumentNullException(nameof(bytes));
            }

            string filePath = path;

            filePath = RemoveInvalidFileNameChars(filePath);
            string dir = Path.GetDirectoryName(filePath);

            if (!System.IO.Directory.Exists(dir))
            {
                if (!createDirIfNotExists)
                {
                    throw new InvalidOperationException($"Directory {dir} was not found");
                }

                System.IO.Directory.CreateDirectory(dir);
            }

            if (File.Exists(filePath))
            {
                if (!overrideFileIfExists)
                {
                    throw new InvalidOperationException($"File {filePath} already exists");
                }

                filePath = GetFilePathWithUniqueFileName(filePath);
            }

            await AsyncFile.WriteAllBytesAsync(filePath, bytes, cancellationToken);

            return(filePath);
        }