C# Program to Write a File using Stream Writer

Here is a sample program as how to write a file using stream writer…in c sharp….
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication20
{
    class FileWrite
    {
        public void WriteData()
        {
            {
                FileStream fs = new FileStream(“C://sohan.txt”, FileMode.Append, FileAccess.Write);
                StreamWriter sr = new StreamWriter(fs);
                Console.WriteLine(“Enter a string”);
                string str = Console.ReadLine();
                sr.Write(str);
                sr.Flush();
                Console.WriteLine(“Writeen Successfully”);
                sr.Close();
                fs.Close();
            }
        }
        public static void Main(String[] args)
        {
            FileWrite fr = new FileWrite();
            fr.WriteData();
            Console.ReadLine();
        }
    }
}

Add Comment