File and FileInfo Classes in C#

By
These Classes can be used to do some manipulation and to set some information regarding the File.
File Class:- File class provided Static Method.It means that there is no need to create the object of the class for access the class Methods.we already know that static member  can access through  the class directly.
FileInfo Class:- FileInfo Class provided the Non Static Method.It means that we will need to create the object of the class  for access  class methods. Class is loaded in memory when the object of the class will be created.
Example of File class:-
There are some steps which is give below.

Step :1  First Open your visual studio-->File-->New-->Project-->Windows forms Application -->click OK-->And make the design which is given below:
see it:


Step :2 Now Double click on Submit button and Write the following codes:

using System;
using System.Windows.Forms;
using System.IO;
namespace Fileinfo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (File.Exists(textBox1.Text))
            {
                MessageBox.Show("File is already exist");
                textBox2.Text = File.ReadAllText(textBox1.Text);
            }
            else
            {
                File.Create(textBox1.Text);
                MessageBox.Show("File created successfully");
            }
        }
    }
}
Step :3 Now Run the program and Enter Drive Path with File Name(textBox1.Text)-->click the Submit button.You will see:-
  •  if File is already Exist then values of that File will display in TextBox(textBox2.Text).
  • If File is not Exist in Drive then New File will be created.
Output:-


Example of FileInfo class:-
FileInfo class has some steps which is similar to File Class.you can download and run this Application in you visual studio directly.
Example:

using System;
using System.Windows.Forms;
using System.IO;
namespace Fileinfo_class
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            FileInfo finfo = new FileInfo(textBox1.Text);
            if (finfo.Exists)
            {
                MessageBox.Show("File is already exist");
                textBox2.Text = finfo.OpenRead().ToString();
            }
            else
            {
                finfo.Create();
                MessageBox.Show("File created success fully");
            }
        }
    }
}
Output:-

Note:-In this above example i have created FileInfo class object(finfo) hat is used for access the class members. For more.


Click below for download whole Application
             Download

0 comments:

Post a Comment

Powered by Blogger.