Create, Read and Write a Text file in C#

In .NET Framework , the "System.IO.File" class library provides static methods for creating, reading, copying, moving, and deleting files. Here you can find few sample code snippets to Create,Read and Write a Text file in C#.

Create a Text file and Writing data to it in C#

class TextApp
    {
        static void Main(string[] args)
        {
            string fileName = @"F:\Test\Example.txt";

            // create a writer and open the file
            TextWriter tw = new StreamWriter(fileName);
 
            // write a line of text to the file
            tw.WriteLine(DateTime.Now);
 
            // close the stream
            tw.Close();
        }
    }


Read the data from a Textfile in C#

class TextApp
    {
        static void Main(string[] args)
        {
            string fileName = @"F:\Test\Example.txt";

            // create reader & open file
            TextReader tr = new StreamReader(fileName);
 
            // read a line of text
            Console.WriteLine(tr.ReadLine());
 
            // close the stream
            tr.Close();
        }
    }

Different ways to write to a file in C# .NET

string fileName = @"F:\Test\Example.txt";
using (FileStream target = File.Open(fileName , FileMode.Append, FileAccess.Write))
{
    using (StreamWriter writer = new StreamWriter(target))
    {
        writer.WriteLine("Hello world");
    }
}

string fileName = @"F:\Test\Example.txt";
    using (MemoryStream memory = new MemoryStream())
    {
        using (StreamWriter writer = new StreamWriter(memory))
        {
            writer.WriteLine("Hello world from memory");
            writer.Flush();
            using (FileStream fileStream = File.Create(fileName ))
            {
                memory.WriteTo(fileStream);
            }
        }
    }

What is GST

GST (Goods and Service Tax) is a comprehensive tax on the supply of goods and services at each stage of any transaction. Read More

Income Tax Information

An income tax is a tax imposed by government on income earned by you. Income tax is a key source of funds that the government uses to fund its activities and serve the public. Read More

General Insurance Companies in India

General insurance is insurance for valuables other than our life and health. General insurance covers the insurer against damage, loss and theft of your valuables. Read More



Types of Bank Loans In India

Loan means lending money from one individual or entity to another. A loan has three components – principal or the borrowed amount, rate of interest and tenure or duration for which the loan is availed. Read More

List of Banks in India

The Reserve Bank of India is the central Bank that is fully owned by the Government of India. It is governed by a central board (headed by a Governor) appointed by the Central Government. Read More

List of Educational Institutions in India

The following list of comprehensive websites on higher education in India. These websites will provide detailed information on education system in India. Read More