w3resource logo


C# Sharp Programming Exercises

C# Sharp Exercises: Append some text to an existing file

Secondary Nav

C# Sharp File Handling : Exercise-8 with Solution

Write a program in C# Sharp to append some text to an existing file.
Expected Output :

 Here is the content of the file mytest.txt :                                                                 
 Hello and Welcome                                                                                            
 It is the first content                                                                                      
 of the text file mytest.txt                                                                                  
                                                                                                              
 Here is the content of the file after appending the text :                                                   
 Hello and Welcome                                                                                            
 It is the first content                                                                                      
 of the text file mytest.txt                                                                                  
 This is the line appended at last line. 

C# Sharp Code:

using System;
using System.IO;
using System.Text;

class filexercise3
{
    public static void Main()
    {
        string fileName = @"mytest.txt"; 
        try
        {
            // Delete the file if it exists.
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }
			Console.Write("\n\n Append some text to an existing file  :\n");
			Console.Write("--------------------------------------------\n");            
            // Create the file.
            using (StreamWriter fileStr = File.CreateText(fileName)) 
            {
                fileStr.WriteLine(" Hello and Welcome");
                fileStr.WriteLine(" It is the first content");
                fileStr.WriteLine(" of the text file mytest.txt");
            }	            
            using (StreamReader sr = File.OpenText(fileName))
            {
                string s = "";
				Console.WriteLine(" Here is the content of the file mytest.txt : ");
                while ((s = sr.ReadLine()) != null)
                {
                    Console.WriteLine(s);
                }
                Console.WriteLine("");
            }
        using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"mytest.txt", true))
        {
            file.WriteLine(" This is the line appended at last line.");
        }
              using (StreamReader sr = File.OpenText(fileName))
            {
                string s = "";
				Console.WriteLine(" Here is the content of the file after appending the text : ");
                while ((s = sr.ReadLine()) != null)
                {
                    Console.WriteLine(s);
                }
                Console.WriteLine("");
            }          
            
        }
        catch (Exception MyExcep)
        {
            Console.WriteLine(MyExcep.ToString());
        }
    }
}

C# Sharp Practice online:


using System;
using System.IO;
using System.Text;

class filexercise3
{
    public static void Main()
    {
        string fileName = @"mytest.txt"; 
        try
        {
            // Delete the file if it exists.
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }
			Console.Write("\n\n Append some text to an existing file  :\n");
			Console.Write("--------------------------------------------\n");            
            // Create the file.
            using (StreamWriter fileStr = File.CreateText(fileName)) 
            {
                fileStr.WriteLine(" Hello and Welcome");
                fileStr.WriteLine(" It is the first content");
                fileStr.WriteLine(" of the text file mytest.txt");
            }	            
            using (StreamReader sr = File.OpenText(fileName))
            {
                string s = "";
				Console.WriteLine(" Here is the content of the file mytest.txt : ");
                while ((s = sr.ReadLine()) != null)
                {
                    Console.WriteLine(s);
                }
                Console.WriteLine("");
            }
        using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"mytest.txt", true))
        {
            file.WriteLine(" This is the line appended at last line.");
        }
              using (StreamReader sr = File.OpenText(fileName))
            {
                string s = "";
				Console.WriteLine(" Here is the content of the file after appending the text : ");
                while ((s = sr.ReadLine()) != null)
                {
                    Console.WriteLine(s);
                }
                Console.WriteLine("");
            }          
            
        }
        catch (Exception MyExcep)
        {
            Console.WriteLine(MyExcep.ToString());
        }
    }
}

Improve this sample solution and post your code through Disqus.



Join our Question Answer community to learn and share your programming knowledge.