Thursday, 17 May 2018

File Stream in C# (How to read /write a text file in C#)





using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Text;

using System.Threading.Tasks;



namespace ConsoleApplication21

{

    class Program

    {

        static void Main(string[] args)

        {

            FileStream F = new FileStream(@"C:\Users\salman\Desktop\FILE\salman.txt",FileMode.OpenOrCreate);

            Console.WriteLine("Enter the text :");

            string s = Console.ReadLine();

            byte[] ascii = Encoding.ASCII.GetBytes(s);

            foreach (byte item in ascii)

            {

                F.WriteByte(item);

            }



            Console.WriteLine("Data successfully written in file named as salman.txt");



            F.Close();





            Console.WriteLine("Press any key to read the same file............");

            Console.ReadKey();

            FileStream F2 = new FileStream(@"C:\Users\salman\Desktop\FILE\salman.txt", FileMode.OpenOrCreate);

            Console.ForegroundColor = System.ConsoleColor.Green;

            int i = 0;

            while ((i=F2.ReadByte())!=-1)

            {

                Console.Write((char)i);

            }

            Console.ReadKey();

            F2.Close();

       



        }

    }

}


No comments:

Post a Comment

Pass Dynamically Added Html Table Records List To Controller In Asp.net MVC

Controller Code: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using ...