Home » .Net » C# file handling programs

C# program to write text to a file

In this C# program, we are going to learn how to write text in a file? Here, we are using Method WriteAllText() of "File" class to write text in a file.
Submitted by IncludeHelp, on October 28, 2017

Given text, and we have to write it into a file using C# program.

File.WriteAllText()

This is a static method of "File" class, which is used to write all text (passed as an argument) in a file. Note: If the file does not exist at specified location, this method creates file first and then write all text to the file.

Syntax:

void File.WriteAllText(path, content);

Parameters:

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

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("Enter text to Write into File : ");
            s = Console.ReadLine();

            File.WriteAllText("ABC.TXT",s);
            Console.WriteLine("\nFile Creation Done");
        }
    }
}

Output

Enter text to Write into File :
Hello , This is a sample program for writing text into file.

File Creation Done

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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.