Monday, 15 July 2013

CSHARP FILE HANDLING REAL APPLICATION - K MANOHAR GOUD

9866550012

File Handling Real Application in C#

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:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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:-
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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:



















26 comments:

  1. I really like your blog. Keep up the good work going.
    visit:
    VIDEOCON BULK SMS SERVICE

    ReplyDelete
  2. I am really happy to say it’s an interesting post to read . I learn new information from your article , you are doing a great job . Keep it up

    YOU MAY VISIT: bulk sms service provider in laxmi nagar

    ReplyDelete
  3. Useful and Informative... Thanks for sharing.I would also like to share some useful information.If you are Looking for the satta result Delhi Darbar in delhi,ncr or satta result Gali, satta result Deshawar. Then open the below link for more details.

    satta King Taj
    satta king online
    satta king online result
    sattaking
    satta king
    satta leak fix number site

    ReplyDelete
  4. your article is so convincing that I never stop myself to say something about it, good going
    you may also visit:
    digital marketing company in delhi

    digital marketing agency in delhi

    digital marketing companies in delhi

    ReplyDelete

  5. Which is the best washing machine cover in 2020? This is the most awaited question to be answered. Washing machine cover are prominent part of every home that helps you in looking neat and gorgeous. Best Washing Washing Machine Cover Whirlpool

    ReplyDelete
  6. This blog has been amazing, the author has really worked hard to form such an informative blog.
    I really appreciate the author for his time. and here is something you would like about the home appliance cover,
    here at dream care we are providing Pillow Cover that too on 70% off shop now!

    ReplyDelete
  7. This blog has been amazing, the author has really worked hard to form such an informative blog.I really appreciate the author for his time. and here is something you would like about the home appliance cover,here at dream care we are providing tv cover online that too on 70% off shop now!

    ReplyDelete
  8. This blog has been amazing, the author has really worked hard to form such an informative blog.I really appreciate the author for his time. and here is something you would like about the home appliance cover,here at dream care we are providing fridge protector that too on 70% off shop now!

    ReplyDelete
  9. Useful and Informative... Thanks for sharing.

    table cover

    ReplyDelete
  10. This is really helpful. You’re doing a great job, Keep it up

    Top hair extensions

    ReplyDelete
  11. This is really helpful. You’re doing a great job, Keep it up

    Buy hair extensions online

    ReplyDelete
  12. hey you are knowledgble informmation .thanks for it .
    if buytable cover plastice then lick on link.

    ReplyDelete
  13. i am looking on your websit. you write very helpful informatione and knowledge key .thanks for it .if you buy and see table cover size then you click on link .

    ReplyDelete
  14. there are in your website article very helpful to me .gues if you intreste buycenter table cover


    then click on my link.thanks

    ReplyDelete
  15. such a nice blog keep sharing it click here for best cushion covers online india

    ReplyDelete
  16. such a nice blog keep sharing it click here for best saree cover online

    ReplyDelete
  17. such a nice blog keep sharing it click here for best RO Cover

    ReplyDelete
  18. such a nice blog keep sharing it click here for best bottle cover 20 litre

    ReplyDelete
  19. This comment has been removed by the author.

    ReplyDelete
  20. I must recommend your blog, that is such a nice blog and if you want to buy cushion protective covers then click on the link.

    ReplyDelete
  21. Nice your Blog

    Massive Male Plus UPDATE 2020 – Does It Really Work?

    https://k12.instructure.com/eportfolios/7387/Home/Massive_Male_Plus_UPDATE_2020__Does_It_Really_Work
    https://angel.co/company/massive-male-plus-male-enhancement-1
    https://www.xfactory.io/forum/she-qun-huo-dong-community-events/massive-male-plus-update-2020-does-it-really-work
    https://in.pinterest.com/pin/811633164091918022/
    https://in.pinterest.com/pin/811633164091918048/
    https://pubhtml5.com/otro/nizh
    https://pubhtml5.com/otro/khwn
    https://www.completefoods.co/diy/recipes/massive-male-plus-male-enhancement-5
    https://www.emailmeform.com/builder/form/759OeBd7SJf5t7i1

    ReplyDelete