Thursday 11 April 2013

FILE HANDLING IN C# (C SHARP)

FILING:

FILES store persistent objects. Objects in memory cease to exist when a program ends. But files exist until deletion. They are handled with the System.IO namespace and the types there. Often they cause errors and performance problems.


STREAM WRITER:

When we made a streamwriter and give a specific path then the file is automatically made on that path.
You van also use TEXTWRITER and TEXTREADER to handle the text.
EXAMPLE:
TextWriter tw = new StreamWriter(@"D:\phone.txt")


STREAM READER:

 StreamReader SR = new StreamReader(@"D:/phone.txt");
           line1 = SR.ReadLine();

OR

( StreamReader SR = new StreamReader("file.txt"))
 {
     string line;
     while ((line = SR.ReadLine()) != null)
     {
  // Do something with line
  string[] parts = line.Split(',');
     }
 }

READALLTEXT:

This simple program uses the File.ReadAllText method to load in the file "file.txt" on the C: volume. Then it prints the contents of the file, which are now stored in a string object.
EXAMPLE:
string text = File.ReadAllText(@"D:\phone.txt");

READALLLINES:

Here we read all the lines from a file and place them in an array. This code reads all lines in "file.txt" with File.ReadAllLines. This is efficient code. It avoids unneeded operations.
EXAMPLE:
string[] lines = File.ReadAllLines("file.txt");

EXAMPLE:
Following is my Code for a phone directory in which aal the numbers and adress are stored in a text file on a given path.. and you can also search specific number and address by entering your desired name.
You can make changes in this Program:
Post anything... We also make programs free... 100% FREE

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace phone
{
    class FileCreate
    {
        public static TextWriter tw;
        public string text;
        public string line1;
        public int cnt;
        public FileCreate()
        {
            {
                tw = new StreamWriter(@"D:\phone.txt");
                //cnt = 1;
            }
        }        
    }
    class Telephone_Directory : FileCreate
    {
        string name, city, phone_number;
        public Telephone_Directory()
        {

        }
        public void RegisterUser()
        {            
            Console.WriteLine("Registering User\n\n");
            Console.WriteLine("Enter Name of the user?");
            name = Console.ReadLine();
            Console.WriteLine("Enter City of User?");
            city = Console.ReadLine();
            Console.WriteLine("Enter the Phone number of the User?");
            phone_number = Console.ReadLine();
           
                tw.WriteLine(name);
                tw.WriteLine(city);
                tw.WriteLine(phone_number);         
        }
        public void SearchUser()
        {
           StreamReader reader = new StreamReader(@"D:/phone.txt");
           line1 = reader.ReadLine(); // readline will read the line and go to next line like Console.WriteLine
            //text = File.ReadAllText(@"D:\phone.txt");
            Console.WriteLine("Search User\n\n");
            Console.WriteLine("Enter the Name of the User?");
            name = Console.ReadLine();

            A: if (line1 == name)
            {
                Console.WriteLine("Name: {0}",line1);
                line1 = reader.ReadLine(); // goes to next line and read the text
                Console.WriteLine("Adress: {0}",line1);
                line1 = reader.ReadLine();
                Console.WriteLine("Phone : {0}",line1);
                //var lines = text.Split(new char[] { '\n' });           // TO COUNT THE LINES
                //var line = lines.Count();
            }
            if(line1 != name)
            {
                line1 = reader.ReadLine();
                goto A; // A is given above
            }             
        }       
        public void show()
        {
            Console.WriteLine("the number of lines are {0}");             
        }
    }
    class Program
        {
              Telephone_Directory t = new Telephone_Directory();            
            public void all()
            {               
                Console.WriteLine("What do You Want?\n1.Register User\n2.Search User\n3.End");
                int ask = Convert.ToInt32(Console.ReadLine());
                if (ask == 1)
                {
                    t.RegisterUser();                   
                    all();                   
                }
                if (ask == 2)
                {
                    FileCreate.tw.Close();
                    t.SearchUser();
                   
                    t.show();
                    all();                  
                }
                if (ask == 3)
                {
                    FileCreate.tw.Close();
                }
            }         
         
            static void Main(string[] args)
            {
                Program p = new Program();
                p.all();            
            }
        }
    }
THANK YOU VERY MUCH FOR YOUR VISIT...

Hit the Facebook like button below if you like this article... this helps a lot and keep us motivated :) 

No comments:

Post a Comment