In this article, we will be discussing different approaches to appending to a file in the C# programming language. Appending to a file is adding data to an existing file on our device. The “System.IO” and “System.Text” namespaces, which include the Stream Writer and Stream Reader classes, will carry out the idea of adding to a file in the C# programming language.

File.AppendText() Method

This method is used to append data into files that already exist, and if the file does not exist, it will create a new file and add text to it. The File.AppendText method is present in the System. IO namespace’s class Stream writer can be directly called in a single line of code. The syntax for this method in the C# programming language is written below:

# “public static System.IO.StreamWriter AppendText (string filepath);”

As you can see in the above code, the namespace and the class of the function are called directly before the function making it independent of past initialization at the start of the program. Then the name of the function is stated, and, in the parameter, the string variable is passed, which has the file path stored in it. This piece of code can directly append to a file.

Now we will implement this method with several alterations and other approaches as well to append a text file in the C# programming language using the Ubuntu 20.04 environment.

Example 01: Using File.Append All Text to Append Text in an Already Existing File in Ubuntu 20.04

In this illustration, we will be using the Stream Writer class of the System.IO namespace, which has several methods related to input functions. We will be utilizing one of these functions from the Stream Writer class, the “File.Append All Text”, this function accepts two arguments for appending to an existing file.

<img alt="A picture containing text Description automatically generated" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/a-picture-containing-text-description-automatical.png" data-lazy- height="90" src="data:image/svg xml,” width=”628″>

In the above C# code, we have written the “File.Append All Text” method with its parameters and all requirements. First, we gave the path to the text file in which we wanted to append the text in the text file, and then we wrote the data that we wanted to append in the text file along with the Environment.New Line function, which will assist us in adding this line to a file at the given path.

After compiling and executing the above program, we will be getting the output from the below screenshot:

<img alt="Text Description automatically generated" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/text-description-automatically-generated.png" data-lazy- height="55" src="data:image/svg xml,” width=”454″>

The message “Text appended” appears on this output screen, indicating that the text was successfully appended to the text file. We will check this by looking for and opening the text file.

<img alt="A picture containing graphical user interface Description automatically generated" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/a-picture-containing-graphical-user-interface-des.png" data-lazy- height="119" src="data:image/svg xml,” width=”723″>

As we can see in the text file, a new line is added through the “File.Append All Text” method.

Example 02: Using the Stream Writer Class to Call the File.Append Text in Ubuntu 20.04

In this illustration, we will be using the Stream writer class’s object to call the File.Append Text function, which is present in the System.IO namespace, will be used to append text to a file that already exists on our device.

<img alt="A screenshot of a computer Description automatically generated with medium confidence" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/a-screenshot-of-a-computer-description-automatica.png" data-lazy- height="151" src="data:image/svg xml,” width=”540″>

In this C# code, we have created an object of the Stream writer class and called the File.Append Text function with the name of the file and its path as the parameter within the using tag. Then we added the appended line by calling the object with the Write Line function within the user tag. In the end, we also printed the success message for this operation that will be visible if the program runs successfully, and we can also see this message on our output screen below:

<img alt="A screenshot of a computer Description automatically generated with medium confidence" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/a-screenshot-of-a-computer-description-automatica-1.png" data-lazy- height="53" src="data:image/svg xml,” width=”528″>

Now we will verify the working of the program by opening the text file that has the appended data in it.

<img alt="Graphical user interface, application Description automatically generated" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/graphical-user-interface-application-description.png" data-lazy- height="135" src="data:image/svg xml,” width=”727″>

Example 03: Using Stream Writer Class to Create a New Text File and Append Text to it in Ubuntu 20.04

In this instance, we will be using two classes of the System.IO namespace. The methods of the Stream Reader class would be used for output functions, and the methods of the Stream Writer class would be used for input functions to a text file. The stream writer class object will be used to append text into a text file in this example.

<img alt="Text, letter Description automatically generated" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/text-letter-description-automatically-generated.png" data-lazy- height="234" src="data:image/svg xml,” width=”448″>

In this C# program, we will be initializing a string variable that will have the name of the text file, and it will be used throughout the program. Then we will be creating a new text file by making an object of the Stream Writer class, which will call the “File.Create Text” function and will have the string variable as a parameter that has the file information; this will add a line in the new text file.

Then we will use the same object to call the “File.Append Text” function, which will also take the string variable as a parameter with the file information; this will append a new line in the newly created text file. After this, we will use the Stream Reader class to open the file by using the File.Open Text function, and by using a string variable and while loop, it will print all the content of the text file on the output screen. Now we will look at the output of this program on our Ubuntu command line terminal.

<img alt="Text Description automatically generated" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/text-description-automatically-generated-4.png" data-lazy- height="73" src="data:image/svg xml,” width=”464″>

In the output screen, we can observe that the text file was created with a line and some appended data as the text file contents are visible.

<img alt="Graphical user interface, website Description automatically generated with medium confidence" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/graphical-user-interface-website-description-aut.png" data-lazy- height="128" src="data:image/svg xml,” width=”726″>

As we can see in the above screenshot of the text file, two lines are present that prove the success of the “File.Append Text” function.

Example 04: Using the Independent Append Function of the System.IO Namespace to Append Text in a Newly Created File in Ubuntu 20.04

This example will look into the Stream Writer class’s independent call of the append function. This technique demonstrates how the Stream Writer class’s input features operate, and it will be different from the conventional approach to append text to a file in the C# programming language.

<img alt="Text Description automatically generated" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/text-description-automatically-generated-8.png" data-lazy- height="307" src="data:image/svg xml,” width=”623″>

In the above C# program, we have first initialized a string variable with the name of the file and its path. Then with the help of the user tag, we will create an object of the Stream writer class and call the Create function to create a file according to the parameter passed of the file info. Then we call the using tag again and, in this tag, we will call the Stream writer class with the System.IO namespace to create a new object with the string variable of file info and the true state of the file as the parameters of the class. Then we will use the Stream Reader class to read all the content of the file by initiating a while loop to traverse the whole file.

After executing this C# program, we will get the following output:

<img alt="Text Description automatically generated" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/text-description-automatically-generated-10.png" data-lazy- height="72" src="data:image/svg xml,” width=”463″>

In this output screen, we can see the content of the new text file with the appended data. We will also look at the text file from our desktop to verify this

<img alt="Graphical user interface Description automatically generated with medium confidence" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/graphical-user-interface-description-automaticall.png" data-lazy- height="121" src="data:image/svg xml,” width=”726″>

Conclusion

This article has discussed the approaches to appending data to a file in the C# programming language. Different classes in the System.IO namespace were discussed, and their functions assisted us in this scenario. We then implemented this concept with several examples of the use of the classes in the Ubuntu 20.04 environment.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/Author-Image-150×150.jpg629d6bfe9b688.jpg" height="112" src="data:image/svg xml,” width=”112″>

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.