File handling in C#

1)working with system.io namespace is called as file handling 

2)file handling is used to  

 a)read the data (or) to write the data into the files 

 b)to find drive information 

 c)to find directory and file information 

 d)to handle 10 operations like copy, delete, move, rename files & folders etc.

 

3)to work with file handling, .Net introduced 7 important classes 

a)directory 

b)directory info 

c)file 

d)fileinfo 

e)drive info 

f)stream reader 

g)stream writer

 

example on directory, fileinfo, driveinfo classes

 

Open windows forms application project: 

Start->programs->Microsoft visual studio 2010->Microsoft Visual studio 2010->file menu->new-> 

project->select visual c# from installed templates->select windows forms application project

place a combobox, button and 2 listbox control

 

 

using System; 

using System.Collections.Generic; 

using System.ComponentModel; 

using System.Data; 

using System.Drawing; 

using System.Linq; 

using System.Text; 

using System.Windows.Forms;

using System.IO;

 

namespace WindowsFormsApplication10 

{ 

    public partial class Form1 : Form 

    { 

        public Form1() 

        { 

            InitializeComponent(); 

        }

 

        private void Form1_Load(object sender, EventArgs e) 

        {  // code for Form_1 load 

            string[] x = Directory.GetLogicalDrives(); 

            // gets all the drive names (letters) 

            for (int i = 0; i < x.Length; i++) 

                comboBox1.Items.Add(x[i]); 

        }

 

        private void button1_Click(object sender, EventArgs e) 

        { 

            // code for button1-click 

            if (comboBox1.SelectedIndex == -1) 

                MessageBox.Show("select a drive");

            else 

            { 

                DriveInfo dr = new DriveInfo(comboBox1.SelectedItem.ToString()); 

                MessageBox.Show("this drive is" + dr.DriveType.ToString()); 

                MessageBox.Show("total memory:" + dr.TotalSize); 

                MessageBox.Show("total free memory:" + dr.TotalFreeSpace); 

            } 

        }

 

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 

        { 

            // code for comboBox1_SelectedIndexChanged 

            string[] x = Directory.GetDirectories(comboBox1.SelectedItem.ToString()); 

            listBox1.DataSource = x; 

        }

 

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e) 

        { 

          // code for listBox1_SelectedIndexChanged 

            string[] x = Directory.GetFiles(listBox1.SelectedItem.ToString()); 

            listBox2.Items.Clear();

 

            for (int i = 0; i < x.Length; i++) 

            { 

                FileInfo k = new FileInfo(x[i]); 

                listBox2.Items.Add(k.Name); 

            } 

        } 

    } 

}

 

Execute the project F5

 

Working with streamreader and streamwriter classes: 

1)streamreader is used to read the data from the files 

2)streamwriter is used to write the data into the files

 

Methods of streamreader & streamwriter classes 

1)streamreader is used to read the data from the file 

2)streamwriter is used to write the data into the file

 

What is Dock ? 

1)This property is available for all the controls , which are visible at runtime 

2)Dock propery changes the control size with respective to form size

 

What is Anchor ? 

1)this property is available for all the controls, which are visible at runtime 

2)anchor property changes the location of the control with respective to from size