/// <summary> /// Appends lines to a file. If the file doesn't exist, this method creates the file, writes the lines to the it, and then closes the it. /// </summary> /// <param name="file">The file.</param> /// <param name="contents">The contents.</param> /// <returns>Whether the write was successful or not.</returns> public static bool AppendAllLines(AFile file, IEnumerable<string> contents) { Exceptions.NotNullException<AFile>(file, nameof(file)); Exceptions.NotNullException<IEnumerable<string>>(contents, nameof(contents)); if (!file.IsOpen) { var str = new StringBuilder(); foreach (var line in contents) str.AppendLine(line); return file.WriteAll(str.ToString(), WriteMode.Append); } return false; }
/// <summary> /// Writes content to the file. If the file does not exist, this method will create the file, write to the file, and then close the file. /// </summary> /// <param name="file">The file.</param> /// <param name="contents">The contents.</param> /// <param name="writeMode">The write mode.</param> /// <returns>Whether the contents were written or not.</returns> public static bool WriteAllText(AFile file, string contents, WriteMode writeMode = WriteMode.Truncate) { Exceptions.NotNullException<AFile>(file, nameof(file)); Exceptions.NotNullException<string>(contents, nameof(contents)); return file.WriteAll(contents, writeMode); }
/// <summary> /// Writes content to the file. If the file does not exist, this method will create the file, write to the file, and then close the file. /// </summary> /// <param name="file">The file.</param> /// <param name="contents">The contents.</param> /// <param name="writeMode">The write mode.</param> /// <returns>Whether the contents were written or not.</returns> public static bool WriteAllBytes(AFile file, byte[] contents, WriteMode writeMode = WriteMode.Truncate) { Exceptions.NotNullException<AFile>(file, nameof(file)); Exceptions.NotNullException<byte[]>(contents, nameof(contents)); return file.WriteAll(Convert.ToString(contents), writeMode); }
/// <summary> /// Writes content to the file. If the file does not exist, this method will create the file, write to the file, and then close the file. /// </summary> /// <param name="file">The file.</param> /// <param name="contents">The contents.</param> /// <param name="writeMode">The write mode.</param> /// <returns>Whether the contents were written or not.</returns> public static bool WriteAllLines(AFile file, List<string> contents, WriteMode writeMode = WriteMode.Truncate) { Exceptions.NotNullException<AFile>(file, nameof(file)); Exceptions.NotNullException<List<string>>(contents, nameof(contents)); var str = new StringBuilder(); foreach (var line in contents) str.AppendLine(line); return file.WriteAll(str.ToString(), writeMode); }
/// <summary> /// Appends text to a file. If the file doesn't exist, this method creates the file, writes the text to the it, and then closes the it. /// </summary> /// <param name="file">The file.</param> /// <param name="contents">The contents.</param> /// <returns>Whether the write was successful or not.</returns> public static bool AppendAllText(AFile file, string contents) { Exceptions.NotNullException<AFile>(file, nameof(file)); Exceptions.NotNullException<string>(contents, nameof(contents)); return file.IsOpen ? false : file.WriteAll(contents, WriteMode.Append); }