Directory and DirectoryInfo Classes in C#

By
Directory and DirectoryInfo  Classes can be used to get some information or for doing some manipulation regarding Directory.This classes are basically  used for Directory Manipulation.
  • Directory Class Provide Static Method.
  • DirectoryInfo class provide Non Static Method.
Example of Directory Class:- There are some steps to understand the Directory Class in C# Which is given below:
Step :1 First open Your Visual studio--> File-->New-->Project->Windows Forms Application-->OK.--> Drag and Drop controls on form  like given below:

Step :2 Now Double click on Submit Button and write the following codes which is given below:

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

        private void button1_Click(object sender, EventArgs e)
        {
            if (Directory.Exists(textBox1.Text))
            {
                String[] sfile = Directory.GetFiles(textBox1.Text);
                foreach (String s in sfile)
                {
                    comboBox1.Items.Add(s);
                }
            }
            else
            {
                Directory.CreateDirectory(textBox1.Text);
                MessageBox.Show("Directory created");
            }
        }
    }
}
Step :3 Now Run the Program(Press F5).
OUTPUT:

Description:-Here i have entered Existing Directory(os) and click the submit button ,then sub directory under Directory(os) will be showed in the comboBox .

Note:- You can use Asp.NET Empty Website instead of Windows Forms Application. Both can be used to perform File handling concepts.

Example of DirectoryInfo Class:- There are also some steps please follow it which is given below:
Step :First open Your Visual studio--> File-->New-->Project->Windows Forms Application-->OK.--> Drag and Drop controls on form  like given below:

Step :Now Double click on Submit Button and write the following codes which is given below:

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

        private void button1_Click(object sender, EventArgs e)
        {
            DirectoryInfo dinfo = new DirectoryInfo(textBox1.Text);
            if (dinfo.Exists)
            {
                FileInfo[] sinfo = dinfo.GetFiles();
                foreach (FileInfo s in sinfo)
                {
                    comboBox1.Items.Add(s);
                }
            }
            else
            {
                dinfo.Create();
                MessageBox.Show(" New Directory has been created");
            }
        }
    }
}
Step :Now Run the Program(Press F5).
Output:

Description:- In above Example i have created a New Directory Using DirectoryInfo class in D   drive. In this program i have created the object of DirectoryInfo class because DirectoryInfo class provide Non Static Method.So we need to create the object of DirectoryInfo class. 
Note:- You can use Asp.NET Empty Website instead of Windows Forms Application. Both can be used to perform File handling concepts.
For more

  1. File and File info class
  2. File handling Real Application
  3. File Handling Concepts

I Hope this is helpful for you.
To Get the Latest  Free Updates Subscribe
Click below for download whole application.
               Download


1 comment:

  1. This is a good tutorial. I have a quick question. What are the scenarios where I should Directory and DirectoryInfo classes? Any real time examples would be a great help

    ReplyDelete

Powered by Blogger.