Q:

C# program to read text from a file and append in another file

0

Given a file, and we have to read text from the file and append the text in another file using C# program.

File.AppendAllText()

This is a method of "File" class, it is used to append given 'content' to the file which is specified by the 'path'.

Syntax:

void File.AppendAllText(path, content);

Parameters:

  1. path - Filename with its location.
  2. content - Text to be written into file.
  3.  

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

using System;

//We need to include this namespace for file handling.
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            string s;

            Console.WriteLine("Content before append:");
            s = File.ReadAllText("ABC.TXT");
            Console.WriteLine(s);

            Console.WriteLine("Enter text to append into file:");
            s = Console.ReadLine();

            File.AppendAllText("ABC.TXT", s);

            Console.WriteLine("Content after append:");
            s = File.ReadAllText("ABC.TXT");
            Console.WriteLine(s);

        }
    }
}

Output

Content of file :
Content before append:
Hello , This is a sample program for writing text into file.
Enter text to append into file:
It is new Text.
Content after append:
Hello , This is a sample program for writing text into file.It is new Text.

Note: In above program, we need to remember, when we use "File" class, System.IO namespace must be included in the program.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# file handling (File class) solved programs/examples

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to replace content of one file from oth... >>
<< C# program to read text from a file...