File Handling in C#

By
All the classes related to File Handing Resides in "System.IO" Namespace. Data can be retrieved from Database  and FileDatabase is basically used for handling Data in an Efficient and Secure manner but In case of File  we can not handle data  more efficient like database.  More problem with File is ,we can not handle data operations  securely .The System.IO  Namespace contains Enumerations and classes that  can be used for read and Write Data into the File.File Handling is  a very most important topic ,in this Topic i will give you  more Real Time application ,which will help you understanding the File  Handling concept in C#.First i am going to explain basics of File Handing with one Real application.Which is given below:
File mode Enumeration:
There are some modes of File which is mostly used in Real application.

  1. Create :-In this mode New File will be created .If  File is Exist then It will be Overridden.
  2.  Append :-  In Append mode Existing File will be open and contents will be written from Last .If File  is not Exist then new File will be created. Append mode is used Between two Files.
  3. Create New:-In this mode New File will be created, if File is already Exist then it will thrown Exception.
  4. Open:- In this Existing file will be opened if file  is not Exist then it will be thrown Exception. 
  5. OpenRead:- In this Opens the Existing File for Reading the Contents.
  6. Truncate:-In this  Existing File will be open and the Entire contents of the file will be removed.But if the file is not Exist then it will be thrown Exception.
 There are some important classes which is used in File Operations.I will discuss each of them through example latter.
  • File Stream:- It supports both Read and Write operations in a File.
  • File Info:-It supports creation,deletion open and moving of files using File Stream object.We can not inherit File Info class from other class.
  • Directory Info:- It is same as File Info class but it is used for Directory operations(create,move,enumerate etc.).We can not inherit Directory Info class from other class.
  • BinaryReader:- It supports to Read Primitives Data Types same as binary values . 
  • BinaryWriter:- it supports to write the primitive Data Types in Binary to a stream.
  • StreamReader:- It supports to Read  characters from Byte stream.
  • StreamWriter:-It supports to Write characters in the string Buffer.
  • TextReader:-It is used for reading the  sequential series of characters.
  • TextWriter:-It is used for writing the  sequential series of characters.
  • MemoryStream:- It supports to creates a stream in Memory .
  • DriveInfo:- It supports to access the information from a drive.
  • FileAccess:-It supports Read,Write access to a File.
 1. StreamReader Class:-
  • Flush():- Flush function is used for immediately save the File contents from Buffer to Memory.
  • Close():-Close function is used for closing the file.If we do not write close() statement in our program then File will be always open mode ,then No other person will be used this File at that time. This is the reason for using close statement in  the File Program.
  • Read()- It is used for Reading the Value using File Stream.
  • Read-line():-It is used  for Read  the value  using File Stream in a File Line by Line.
  • Peek():- It returns next value but not use it.
  • Seek():- It is used for Read/Write a values at any positions in a file.
2. StreamWriter Class:-
  • close()-->Close function is used for closing the file.
  • Flush():-Flush function is used for immediately save the File contents from Buffer to Memory.
  • Write():- It is used for writing a File using File stream class.
  • WriteLine():- It is used to write a File Line by Line using File stream.
Note :- There are some other classes also but they used rarely in any real Application.
Now i am going to make some real application which include following things which is given below:
  • Writing the Contents in a File.
  • Reading the contents from the File.
  • Finding the Word from the File.
There are some steps to perform this three tasks.Follow step by step which is given below:
Step :1  First open Your visual studio-->File -->New-->Project-->Select Windows Forms Application-->OK.
Step :2 Now make this Design using controls which is as shown below:
see it:
Step :3 Now  double click on Buttons(Write,Read,Find) one by one and write the following codes which is given below:
see it:

using System;
using System.Windows.Forms;
using System.IO;
namespace file_write
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
   FileStream fs = new FileStream(textBox2.Text, FileMode.Create,FileAccess.Write);
         StreamWriter sw = new StreamWriter(fs);
         sw.WriteLine(textBox1.Text);
         sw.Flush();
         fs.Close();
         MessageBox.Show("Content is written in file successfully");
        }
        private void button2_Click(object sender, EventArgs e)
        {
     FileStream fs = new FileStream(textBox2.Text, FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fs);
            textBox3.Text = sr.ReadToEnd();
            fs.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
     FileStream fs = new FileStream(textBox2.Text, FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fs);
            String str = sr.ReadToEnd();
            int i = (str.IndexOf(textBox4.Text,0));
            if (i >-1)
            {
                MessageBox.Show("This word is exist in the file");
            }
            else
            {
           MessageBox.Show("This word is not exist in the file try another words");
            }
        }
    }
}

Description:- Here i have written c# codes for three purpose.which is given below:

  • Button1 click-->Writing the Contents in a File Using c# programming.
  • Button2 click--> Reading the contents from File Using c# programming.
  • Button3 click--> Searching the word from File using c# programming.

Step :Now  Run the program(Press F5). In Upper TextBox (textBox2.Text) -->Write Your Directory Path and File Name-->Now Write the contents which you want to write in TextBox(textBox1.text)-->and click Write Button.You will see which is shown below:
see it:

Now go your Directory path which you have specified ,you will see your File will be created and contents of TextBox  are written in  the File.
Step :5 Now click on Read Button -->You will see,contents are read from File and shown in TextBox(textBox3.Text).
see it:

Step :6  Now Write some words in TextBox(textBox4.Text) for searching purpose and Click Find Button. you will see:

You can download whole Application on click on Download button from the bottom.You can directly run this Application in your system using Visual Studio 2010 or others.
For more...
  1. Directory and Directory Info Class in C#
  2. File and File Info class in C#
  3. File Handling Real Application 
  4. Real Generic collection concepts in c#
  5. How to host asp.net application on iis server
  6. Stored procedure concepts in c#
  7. String and string Builder in c#
  8. How to solve sql server problems in .Net
  9. Call by value and call by reference concepts in c#
  10. Properties concepts in c#
  11. How to create captcha for your website
  12. Abstract class and abstract method concepts in c#
I hope this is helpful for you.
To Get the Latest  Free Updates Subscribe Download Attached Whole Application
             Download

0 comments:

Post a Comment

Powered by Blogger.