File.AppendAllLines(String, IEnumerable, Encoding) Method in C#

Within the expansive realm of C# programming, developers frequently encounter situations where proficient file handling is imperative. Whether it involves logging data, preserving user preferences, or managing application configurations, the capability to write to a file effortlessly is paramount. One method that stands out for its simplicity and effectiveness is the File.AppendAllLines() method.

Belonging to the System.IO namespace in C#, the File.AppendAllLines() method offers a convenient means to append lines to a file. Its distinct utility lies in its ability to add new data to an existing file without overwriting its contents.

Syntax:

It has the following syntax:

  • path: It denotes the path to the file to which lines will be appended.
  • contents: It represents an IEnumerable<string> encapsulating the lines to append.
  • encoding: It specifies the character encoding applied to the contents.

Example:

Now, let's explore a practical example of using File.AppendAllLines() to add lines to a file. Consider a straightforward C# console application that logs user activity:

Output:

Logged: John Doe logged in.
Logged: Jane Smith performed a data update.
Logged: Admin user accessed sensitive information.
 
Log File Contents:
John Doe logged in.
Jane Smith performed a data update.
Admin user accessed sensitive information.

Explanation:

Main Method:

  • Commences the execution of the program.
  • Records user activities through the LogUserActivity
  • Exhibits the contents of the log file via the DisplayLogFile

LogUserActivity Method:

  • Documents a user activity by adding a line to a designated log file.
  • Specifies the log file path as "user_activity.log".
  • Utilizes AppendAllLines to append the given activity to the log file.
  • It conveys a console message signifying the successful logging of the activity.

DisplayLogFile Method:

  • It tries to retrieve and exhibit the contents of a log file.
  • It utilizes ReadAllLines to fetch all lines from the indicated log file.
  • It presents the log file contents on the console in an easily readable format.
  • It captures a FileNotFoundException scenario and displays a pertinent error message.

File Handling:

  • It implements classes from the IO namespace for effective file management, including File, File.AppendAllLines, and File.ReadAllLines.
  • It specifies UTF8 when appending lines to the log file to ensure correct character encoding.

Logging Activities:

  • The program records three distinct user activities: logging in, updating data, and accessing sensitive information.
  • Each activity is appended as a new line to the "user_activity.log"

Displaying Log File Contents:

  • Following the logging of activities, the program reads and presents the contents of the log file.
  • An appropriate error message is displayed if the file is not found (FileNotFoundException).

Console Output:

  • It generates console output to signify the logging of each activity.
  • Subsequently, it showcases the log file's contents on the console.

Conclusion:

In conclusion, the presented C# code illustrates the practical utilization of the File.AppendAllLines() method for streamlined file management. The main program adeptly captures user activities, systematically logging them into a specified file with concise syntax. Employing the System.IO namespace underscores the seamless integration of file management features, particularly focusing on the append operation (File.AppendAllLines()) and content retrieval (File.ReadAllLines()). The inclusion of Encoding.UTF8 ensures both compatibility and appropriate character encoding during the addition of lines to the log file.

Additionally, the program's robustness is exemplified through meticulous exception handling, specifically addressing the potential occurrence of a FileNotFoundException when attempting to read the log file. The code's clarity and strategic implementation of console output contribute to its didactic value, rendering it a valuable reference for developers in search of effective and comprehensible approaches to file-related tasks in C#. In essence, this example serves as an instructive resource, highlighting best practices in file handling and showcasing the flexibility and simplicity inherent in the File.AppendAllLines() method.






Latest Courses