コード例 #1
0
        public async Task <bool> CreateFileAsync(string fileNameString, byte[] contentBytesArray,
                                                 FileCreationType creationType)
        {
            try
            {
                if ((contentBytesArray == null) || (contentBytesArray.Length == 0))
                {
                    return(false);
                }

                await _fileSemaphore.WaitAsync();

                var filePathString = PrepareFilePath(fileNameString);
                if (string.IsNullOrEmpty(filePathString))
                {
                    _fileSemaphore.Release();
                    return(false);
                }

                var fileStreamInfo = PrepareFileStream(filePathString, contentBytesArray, creationType);
                if (fileStreamInfo == null)
                {
                    _fileSemaphore.Release();
                    return(false);
                }

                using (var fileStream = fileStreamInfo.Item1)
                {
                    var streamBytesArray = fileStreamInfo.Item2;
                    if ((streamBytesArray == null) || (streamBytesArray.Length == 0))
                    {
                        return(false);
                    }

                    await fileStreamInfo.Item1.WriteAsync(fileStreamInfo.Item2, 0, fileStreamInfo.Item2.Length);

                    _fileSemaphore.Release();
                }

                return(true);
            }
            catch (DirectoryNotFoundException exception)
            {
                Diagnostics.Debug.WriteLine(exception.StackTrace);
                _fileSemaphore.Release();
                return(false);
            }
            catch (IOException exception)
            {
                Diagnostics.Debug.WriteLine(exception.StackTrace);
                _fileSemaphore.Release();
                return(false);
            }
            catch (Exception exception)
            {
                Diagnostics.Debug.WriteLine(exception.StackTrace);
                _fileSemaphore.Release();
                return(false);
            }
        }
コード例 #2
0
        /// <summary>
        ///     Creates objects needed for the writing process and starts it.
        /// </summary>
        /// <param name="fileCreationType">
        ///     The strategy for creating the excel file.
        ///     It is assumed that the function is only called with <see cref="FileCreationType.UpdateExistingFile" />,
        ///     <see cref="FileCreationType.CreateEmptyFile" /> or <see cref="FileCreationType.CreateNewFile" />
        ///     as possible values.
        /// </param>
        /// <param name="translationsDictionary">
        ///     The translations that should be written into the dictionary.
        /// </param>
        /// <param name="path">The path of the excel sheet.</param>
        private void CreateExelFileBasedOnCreationType(FileCreationType fileCreationType,
            Dictionary<CultureInfo, Dictionary<string, string>> translationsDictionary, string path)
        {
            var excel = new ExcelInterop.Application();
            ExcelInterop.Workbook workbook = null;

            try
            {
                //FileCreationType.UpdateExistingFile
                if (fileCreationType == FileCreationType.UpdateExistingFile)
                {
                    workbook = excel.Workbooks.Open(System.IO.Path.GetFullPath(path));
                }
                //FileCreationType.CreateEmptyFile or FileCreationType.CreateNewFile
                else
                {
                    workbook = excel.Workbooks.Add();
                }

                //FileCreationType.UpdateExistingFile or FileCreationType.CreateNewFile
                if (fileCreationType != FileCreationType.CreateEmptyFile)
                {
                    //get parameters.
                    var worksheet = (ExcelInterop.Worksheet) workbook.Worksheets[1];
                    var textLocalizations =
                        TextLocalizationsUtils.FlipLocalizationsDictionary(translationsDictionary);

                    //write to sheet.
                    WriteTranslationsToWorksheet(worksheet, textLocalizations);
                }

                //saving.
                excel.DisplayAlerts = false;
                //FileCreationType.UpdateExistingFile.
                if (fileCreationType == FileCreationType.UpdateExistingFile)
                {
                    workbook.Save();
                }
                //FileCreationType.CreateEmptyFile or FileCreationType.CreateNewFile.
                else
                {
                    workbook.SaveAs(System.IO.Path.GetFullPath(path));
                }
            }
            finally
            {
                workbook?.Close();
                excel.Quit();
            }
        }
コード例 #3
0
        public bool CreateFile(string fileNameString, byte[] contentBytesArray, FileCreationType creationType)
        {
            try
            {
                if ((contentBytesArray == null) || (contentBytesArray.Length == 0))
                {
                    return(false);
                }

                var filePathString = PrepareFilePath(fileNameString);
                if (string.IsNullOrEmpty(filePathString))
                {
                    return(false);
                }

                var fileStreamInfo = PrepareFileStream(filePathString, contentBytesArray, creationType);
                if (fileStreamInfo == null)
                {
                    return(false);
                }

                using (var fileStream = fileStreamInfo.Item1)
                {
                    var streamBytesArray = fileStreamInfo.Item2;
                    if ((streamBytesArray == null) || (streamBytesArray.Length == 0))
                    {
                        return(false);
                    }

                    fileStream.Write(streamBytesArray, 0, streamBytesArray.Length);
                }

                return(true);
            }
            catch (DirectoryNotFoundException exception)
            {
                Diagnostics.Debug.WriteLine(exception.StackTrace);
                return(false);
            }
            catch (IOException exception)
            {
                Diagnostics.Debug.WriteLine(exception.StackTrace);
                return(false);
            }
        }
コード例 #4
0
        private Tuple <FileStream, byte[]> PrepareFileStream(string filePathString, byte[] contentBytesArray,
                                                             FileCreationType creationType)
        {
            if ((contentBytesArray == null) || (contentBytesArray.Length == 0))
            {
                return(null);
            }

            if (string.IsNullOrEmpty(filePathString))
            {
                return(null);
            }

            FileStream fileStream = null;

            switch (creationType)
            {
            case FileCreationType.Overwrite:
            {
                fileStream = File.Create(filePathString);
                return(new Tuple <FileStream, byte[]>(fileStream, contentBytesArray));
            }

            case FileCreationType.Copy:
            {
                var fileCopyPathString = string.Concat(filePathString, kCopySubscriptString);
                File.Copy(filePathString, fileCopyPathString);
                fileStream = File.Create(filePathString);
                return(new Tuple <FileStream, byte[]>(fileStream, contentBytesArray));
            }

            case FileCreationType.Append:
            {
                var existingBytesArray = (DoesFileExist(filePathString) == true)
                                ? File.ReadAllBytes(filePathString) : null;

                using (fileStream = File.Create(filePathString))
                {
                    if (fileStream == null)
                    {
                        return(null);
                    }

                    byte[] appendedBytesArray = null;
                    if ((existingBytesArray == null) || (existingBytesArray.Length == 0))
                    {
                        appendedBytesArray = new byte[contentBytesArray.Length];
                        contentBytesArray.CopyTo(appendedBytesArray, contentBytesArray.Length);
                        return(new Tuple <FileStream, byte[]>(fileStream, contentBytesArray));
                    }

                    appendedBytesArray = new byte[existingBytesArray.Length + contentBytesArray.Length];
                    existingBytesArray.CopyTo(appendedBytesArray, 0);
                    contentBytesArray.CopyTo(appendedBytesArray, existingBytesArray.Length);
                    return(new Tuple <FileStream, byte[]>(fileStream, appendedBytesArray));
                }
            }
            }

            return(null);
        }
コード例 #5
0
 public async Task <bool> CreateFileAsync(string fileNameString, byte[] contentBytes,
                                          FileCreationType creationType) =>
 (await _fileManager.CreateFileAsync(fileNameString, contentBytes, creationType));
コード例 #6
0
 public bool CreateFile(string fileNameString, byte[] contentBytes, FileCreationType creationType) =>
 (_fileManager.CreateFile(fileNameString, contentBytes, creationType));