Пример #1
0
        /// <summary>
        /// Reads some text from file.
        /// </summary>
        /// <param name="fileName">The name of file</param>
        /// <param name="fileFormat">The file format extension</param>
        /// <param name="filePath">The location of file to write to</param>
        /// <param name="isCreateNewAndSave">If value is true, indicating save file as new file</param>
        /// <returns></returns>

        public async Task <List <string> > ReadTextFromFileAsync(string fileName, FileTypeExtension fileFormat, string filePath, bool isCreateNewAndSave = true)
        {
            var text    = default(string);
            var content = new List <string>();


            try
            {
                filePath = NormalizePath(filePath);

                filePath = ResolvePath(filePath);

                await AsyncEngine.AwaitAsync(nameof(FileManager) + filePath, async() =>
                {
                    await Task.Run(() =>
                    {
                        using (var streamReader = (TextReader) new StreamReader(File.Open($"{filePath}/{fileName}{FileExtensions.FileTypeExtensions(fileFormat)}", FileMode.Open)))
                        {
                            while (streamReader.Peek() > -1)
                            {
                                text = streamReader.ReadLine();

                                content = text.Split(new char[] { '\n' }).ToList();
                            }
                        }
                    });
                });
            }
            catch (Exception Ex)
            {
                Logger.Log($"{Ex.Message}", LogLevel.Error);
            }

            return(content);
        }
Пример #2
0
        /// <summary>
        /// Writes some text to add to the file.
        /// </summary>
        /// <param name="text">The text to write to</param>
        /// <param name="fileName">The name of file</param>
        /// <param name="fileFormat">The file format extension</param>
        /// <param name="filePath">The location of file to write to</param>
        /// <param name="isAppend">If value is true, indicating add text to the end of file</param>
        /// <returns></returns>
        public async Task WriteTextToFileAsync(string fileName, FileTypeExtension fileFormat, string filePath, string text, bool isAppend = false)
        {
            try
            {
                filePath = NormalizePath(filePath);

                filePath = ResolvePath(filePath);

                await AsyncEngine.AwaitAsync(nameof(FileManager) + filePath, async() =>
                {
                    await Task.Run(() =>
                    {
                        using (var streamWriter = (TextWriter) new StreamWriter(File.Open($"{filePath}/{fileName}{FileExtensions.FileTypeExtensions(fileFormat)}", isAppend ? FileMode.Append : FileMode.Create)))
                        {
                            streamWriter.Write(text);
                        }
                    });
                });
            }
            catch (Exception Ex)
            {
                Logger.Log($"{Ex.Message}", LogLevel.Error);
            }
        }