/// <summary> /// Reads all text from the file. /// </summary> /// <param name="fileName">The name of file</param> /// <param name="fileFormat">The format of the file</param> /// <param name="filePath">The location of file</param> /// <param name="text">The text to write to file</param> public static async void ReadTextFromTheFileAsync(string fileName, FileFormatTypeExtension fileFormat, string filePath) { try { SortedListItem = new List <string>(); // Check current path of our file filePath = NormalizePath(filePath); // And resolve path to get absolute filePath = ResolvePath(filePath); using (var fileReader = (TextReader) new StreamReader(File.Open($"{filePath}{"/"}{fileName}{FormatFileMethodExtension.GetValidFileFormat(fileFormat)}", FileMode.Open))) { while (fileReader.Peek() >= 0) { UnsortedListItem.Add(fileReader.ReadLine()); } SortingUnsortedData <string>(UnsortedListItem); } } catch (Exception Ex) { Console.WriteLine($"Message: {Ex.Message}"); } await Task.Delay(TimeSpan.FromSeconds(0.5)); }
/// <summary> /// Writes some text to add to the file. /// </summary> /// <param name="fileName">The name of file</param> /// <param name="fileFormat">The format of the file</param> /// <param name="filePath">The location of file</param> /// <param name="isAppend">If true, indicates adding text to the end of file</param> /// <param name="text">The text to write to file</param> public static async void WriteTextToFileAsync(string fileName, FileFormatTypeExtension fileFormat, string filePath, bool isAppend, string text) { UnsortedListItem = new List <string>(); try { // Check current path of our file filePath = NormalizePath(filePath); // And resolve path to get absolute filePath = ResolvePath(filePath); using (var fileWriter = (TextWriter) new StreamWriter(File.Open($"{filePath}{"/"}{fileName}{FormatFileMethodExtension.GetValidFileFormat(fileFormat)}", isAppend ? FileMode.Append : FileMode.Create))) { UnsortedListItem = text.Split(new char[] { ',', '.', '"', '/', '-', ';', ':', '\n', '\t' }).ToList(); foreach (var item in UnsortedListItem) { fileWriter.WriteLine(item); } // By automatically the data from the list will be sorted SortingUnsortedData <string>(UnsortedListItem); } } catch (Exception Ex) { Console.WriteLine($"Message: {Ex.Message}"); } await Task.Delay(TimeSpan.FromSeconds(0.5)); }
/// <summary> /// Writes some text to add to the file. /// </summary> /// <param name="fileName">The name of file</param> /// <param name="fileFormat">The format of the file</param> /// <param name="filePath">The location of file</param> /// <param name="isAppend">If true, indicates adding text to the end of file</param> /// <param name="text">The text to write to file</param> public static async void WriteTextToFileAsync <T>(string fileName, FileFormatTypeExtension fileFormat, string filePath, bool isAppend, List <T> texts) { try { // Check current path of our file filePath = NormalizePath(filePath); // And resolve path to get absolute filePath = ResolvePath(filePath); using (var fileWriter = (TextWriter) new StreamWriter(File.Open($"{filePath}{"/"}{fileName}{FormatFileMethodExtension.GetValidFileFormat(fileFormat)}", isAppend ? FileMode.Append : FileMode.Create))) { if (texts.Count() <= 0) { return; } foreach (var item in texts) { fileWriter.WriteLine(item); } Console.WriteLine("Good job!, Your data has been written to the file successfully..."); } } catch (Exception Ex) { Console.WriteLine($"Message: {Ex.Message}"); } await Task.Delay(TimeSpan.FromSeconds(0.5)); }