C# Append to File

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.

A picture containing text Description automatically generated

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:

Text Description automatically generated

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.

A picture containing graphical user interface Description automatically generated

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.

A screenshot of a computer Description automatically generated with medium confidence

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:

A screenshot of a computer Description automatically generated with medium confidence

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

Graphical user interface, application Description automatically generated

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.

Text, letter Description automatically generated

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.

Text Description automatically generated

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.

Graphical user interface, website Description automatically generated with medium confidence

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.

Text Description automatically generated

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:

Text Description automatically generated

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

Graphical user interface Description automatically generated with medium confidence

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.



from https://ift.tt/IEdJYih

Post a Comment

0 Comments