File Handling Real Application in C#

By
 Today ,i am going to make a Real Application in File Handling Using C#.You can use this Application to anywhere in your projects.All Real Time Application are made on the behalf  of this small Application. File Handling Application are mostly used in Software ,website and Automation etc. You can download full Application from bottom and Run  this on your system using Visual studio . This Application includes following features which is given below:
  • How to Find Logical Drives in Your System and Find Total and free space in selected Drive using C#
  • How to Create New Directory in your System Drive using C#.
  • How to Create Sub Directory in your system drives in Directory using C#.
  • How to Show Directory's File Details using C# language.
  • How to Copy Directory From One Drive To Another Drive Using C#.
  • How to Write contents in File using C#.
  • How to Read File from system using C#.
  • How to Find (Search)  text data from File using C#.
  • How to Append Contents to the Existing File using C#.
  • How to Rename File Name in your system using C#.
  • How to Replace Old Value to New Value from your Existing file  using C#.
Full Application screenshot is given below:
see it:

There are some steps please follow one by one to understand the whole File Handling concepts.
Step :1 First open your visual studio -->open Windows Forms Application -->drag and drop controls to the form as given below:

Step :2 Now Double click on Show Your system Drive Details  Button  , comboBox  and Write the following code which is given below:
using System;
using System.IO;
using System.Windows.Forms;
namespace File_Handling_Application
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                int i;
                String[] strd = Directory.GetLogicalDrives();
                for (i = 0; i < strd.Length; i++)
                {
                    comboBox1.Items.Add(strd[i]);
                }
            }
            catch (Exception ram)
            {
                MessageBox.Show(ram.Message);
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                String val = comboBox1.SelectedItem.ToString();
                System.IO.DriveInfo dinfo = new System.IO.DriveInfo(val);
    textBox1.Text = "your selected drive is:" + val + "\n Total Size of Drive is:" + dinfo.TotalSize + "\n Available Free space in Drive:" + dinfo.AvailableFreeSpace;
            }
            catch (Exception ram)
            {
                MessageBox.Show(ram.Message);
            }
        }
}
}

Step :3 Now Run the Program(press F5) ,click on Button and select drive from comboBox.
output:
Step :4 Now Drag and drop controls as given below:
see it:

Step :5 Now double click on Buttons and write the following codes which is given below:
private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                if (!Directory.Exists(textBox2.Text))
                {
                    Directory.CreateDirectory(textBox2.Text);
                    MessageBox.Show("Directory created");
                }
                else
                {
              MessageBox.Show("Please Enter correct directory path and File name");
                }
            }
            catch (Exception ram)
            {
                MessageBox.Show(ram.Message);
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                String path = textBox2.Text;
                System.IO.DirectoryInfo sudir = new DirectoryInfo(textBox2.Text);
                String subname = textBox3.Text;
                sudir.CreateSubdirectory(subname);
                MessageBox.Show("SubDirectory has successfully created");
            }
            catch (Exception ram)
            {
                MessageBox.Show(ram.Message);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                String path = textBox2.Text;
                DirectoryInfo dinfo = new DirectoryInfo(path);
                if (dinfo.Exists)
                {
                    DirectoryInfo[] subdir = dinfo.GetDirectories();
                    foreach (DirectoryInfo s in subdir)
                    {
                        comboBox2.Items.Add(s);
                    }

                    FileInfo[] finfo = dinfo.GetFiles("*");
                    foreach (FileInfo f in finfo)
                    {
                        comboBox2.Items.Add(f);
                    }


                }
                else
                {
 MessageBox.Show("Directory does not Exist,Please enter valid directory path and name");
                }
            }
            catch (Exception ram)
            {
                MessageBox.Show(ram.Message);
            }

        }


Step :6 Now Run the program and create Directory,Sub directory and show Directory and files one by one. See your drive, Directory is created or not.
Output:

Step :7 Now Again drag and drop controls as shown below:

Step :7 Now Double click on Copy directory Button and write the following codes.
see it:
private void button5_Click(object sender, EventArgs e)
        {
            try
            {
                DirectoryInfo srcdir = new DirectoryInfo(textBox4.Text);
                DirectoryInfo destdir = new DirectoryInfo(textBox5.Text);
                CopyDirectory(srcdir, destdir);
            }
            catch (Exception ram)
            {
                MessageBox.Show(ram.Message);
            }

        }

        private void CopyDirectory(DirectoryInfo srcdir, DirectoryInfo destdir)
        {
            try
            {
                if (!destdir.Exists)
                {
                    destdir.Create();
                    FileInfo[] finfo = srcdir.GetFiles();
                    foreach (FileInfo f in finfo)
                    {
                        f.CopyTo(Path.Combine(destdir.FullName, f.Name));
                    }
                    DirectoryInfo[] dinfo = srcdir.GetDirectories();
                    foreach (DirectoryInfo d in dinfo)
                    {
                        String dest = Path.Combine(destdir.FullName, d.Name);
                        CopyDirectory(d, new DirectoryInfo(dest));
                    }
         MessageBox.Show("Directory has Copied successfully to another Drive");
                }
                else
                {
                    MessageBox.Show("Please enter valid path and File name");

                }
            }
            catch (Exception ram)
            {
                MessageBox.Show(ram.Message);
            }
            
        }


Step :8 Now Run the Program (press F5)


Step :9 Now drag and drop controls on the Form as shown below:
see it:

Step :10 Now Double Click on Write,Read,Find and Append Button and Write the following code:-
private void button6_Click(object sender, EventArgs e)
        {
            try
            {
     FileStream fs = new FileStream(textBox6.Text, FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(fs);
                String str = sr.ReadToEnd();
                int i = (str.IndexOf(textBox9.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");
                }
            }
            catch (Exception ram)
            {
                MessageBox.Show(ram.Message);
            }
        }

 private void button7_Click(object sender, EventArgs e)
        {
            try
            {
     FileStream fs = new FileStream(textBox6.Text, FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(fs);
                textBox8.Text = sr.ReadToEnd();
                fs.Close();
            }
            catch (Exception ram)
            {
                MessageBox.Show(ram.Message);
            }
        }
 private void button8_Click(object sender, EventArgs e)
        {
            try
            {
  FileStream fs = new FileStream(textBox6.Text, FileMode.Create, FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs);
                sw.WriteLine(textBox7.Text);
                sw.Flush();
                fs.Close();
                MessageBox.Show("Content is written in file successfully");
            }
            catch (Exception ram)
            {
                MessageBox.Show(ram.Message);
            }
        }
 private void button9_Click(object sender, EventArgs e)
        {
            try
            {
                String str = textBox6.Text;
                StreamWriter sw = File.AppendText(str);
                sw.WriteLine(textBox10.Text);
                sw.Close();
                MessageBox.Show("File contents appended successfully");
            }
            catch (Exception ram)
            {
                MessageBox.Show(ram.Message);
            }

         }
Step :11 Now Run the Program(press F5) and Fill the required field which is as shown below:
Output:

Step :12  Now Again drag and drop controls on the Form as given below:
see it:

Step :12 Now double click on Rename ,Replace Button and write the following codes which is given below:
private void button10_Click(object sender, EventArgs e)
        {
            String src = textBox6.Text;
            String dest = textBox11.Text;
            try
            {
                FileInfo srcfile = new FileInfo(src);
                if (srcfile.Exists)
                {
                    srcfile.MoveTo(dest);
                    MessageBox.Show("File is Renamed successfully");
                }
                else
                {
                    MessageBox.Show("Entere correct File Name and Path again");
                }
            }
            catch(Exception ram)
            {
                MessageBox.Show(ram.Message);
            }

        }

        private void button11_Click(object sender, EventArgs e)
        {
            String text = File.ReadAllText(textBox6.Text);

           String Value = text.Replace(textBox12.Text, textBox13.Text);
           File.WriteAllText(textBox6.Text, Value);
           MessageBox.Show("successfully Replaced");


        }
Step :13  Now Run the program (press F5) and fill the required field as shown below:
see it:


Note :-  I have built this Application,you can download whole Application with codes.You can understand this Whole application when you will run this application on your system.
For more

  1. Collections in c#
  2. Generic Collection in C#
  3. File and File Info Class

I Hope  this is helpful for You.
Click Below for Download whole Application
                   Download

3 comments:

  1. good ..and how to connect default sql server in visual studio 2010 in win forms applications...

    ReplyDelete
  2. @ SATHISH follow this link:http://www.msdotnet.co.in/2013/04/how-to-solve-problem-to-add-sql.html

    ReplyDelete
    Replies
    1. @Ramashanker excellent articles. Thanks for all the efforts mate.

      I need a treeview in master page and want to retain its expanded state if any node is clicked or any postback from the content page. Can you please help me out in this?

      Delete

Powered by Blogger.