Exemplo n.º 1
0
        /// <summary> Writes a file asynchronous. </summary>
        /// <exception cref="ArgumentNullException"> Thrown when one or more required arguments are null. </exception>
        /// <exception cref="ArgumentOutOfRangeException"> Thrown when one or more arguments are outside
        ///     the required range. </exception>
        /// <exception cref="IOException"> Thrown when an IO failure occurred. </exception>
        /// <param name="fileBytes"> The file in bytes. </param>
        /// <param name="filePath"> Full pathname of the file. </param>
        /// <param name="existingFileCheck"> (Optional) The existing file check. </param>
        /// <returns> A Task&lt;bool&gt; </returns>
        public async Task <bool> WriteFileAsync(byte[] fileBytes, string filePath, ExistingFileOption existingFileCheck = ExistingFileOption.DoNotCheck)
        {
            if (fileBytes == null)
            {
                throw new ArgumentNullException(nameof(fileBytes));
            }
            if (filePath == null)
            {
                throw new ArgumentNullException(nameof(filePath));
            }
            if (String.IsNullOrWhiteSpace(filePath))
            {
                throw new ArgumentOutOfRangeException(nameof(filePath));
            }

            bool result = false;

            bool existingFileFound = (existingFileCheck != ExistingFileOption.DoNotCheck) && FileExists(filePath);

            if (existingFileFound && existingFileCheck == ExistingFileOption.ThrowException)
            {
                throw new IOException($"The file cannot be created because a file already exists with the specified path: {filePath}");
            }
            else if (existingFileFound && existingFileCheck == ExistingFileOption.ReplaceExisting)
            {
                await DeleteFileAsync(filePath);

                existingFileFound = false;
            }

            if (!existingFileFound)
            {
                await Task.Run(() => File.WriteAllBytes(filePath, fileBytes));

                result = true;
            }

            return(result);
        }
Exemplo n.º 2
0
 /// <summary> Writes a file asynchronous. </summary>
 /// <param name="fileBytes"> The file in bytes. </param>
 /// <param name="fileName"> Filename of the file. </param>
 /// <param name="appDataSubFolder"> Pathname of the application data sub folder. </param>
 /// <param name="existingFileCheck"> (Optional) The existing file check. </param>
 /// <returns> A Task&lt;bool&gt; </returns>
 public async Task <bool> WriteFileAsync(byte[] fileBytes, string fileName, string appDataSubFolder, ExistingFileOption existingFileCheck = ExistingFileOption.DoNotCheck)
 {
     return(await WriteFileAsync(fileBytes, AppDataFilePath(fileName, appDataSubFolder), existingFileCheck));
 }