C# DirectoryInfo Tutorial with Programming Examples and Codes

In this chapter you will learn:
  • What is DirectoryInfo class in C#?
  • How to get Directory details using DirectoryInfo class?
  • Programming Examples and codes.

What is DirectoryInfo class in C#?

DirectoryInfo class allows you to work with directory and its make directory manipulation as create, delete, info etc easy. It exposes instance methods for creating, moving, enumerating through directories and subdirectories.

Notes

  1. DirectoryInfo class is used for typical operations such as copying, moving, creating or deleting directories.
  2. This class cannot be inherited.
  3. By default full read/write access to new directories is granted to all users.

How do I get Directory Details?

The given example will demonstrate well DirectoryInfo class. This program check for a directory "D:\csharp" and If directory will be there it will show information of directory else it will create a new directory D:\csharp

 

Programming Examples and Codes

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace DirectoryInfo_class
{
    class Program
    {
        static void Main(string[] args)
        {
            string path=@"D:\csharp1";
            DirectoryInfo dir = new DirectoryInfo(path);
            try
            {
                if (dir.Exists)
                {
                    Console.WriteLine("{0} Directory is already exists", path);
                    Console.WriteLine("Directory Name : " + dir.Name);
                    Console.WriteLine("Path : " + dir.FullName);
                    Console.WriteLine("Directory is created on : " + dir.CreationTime);
                    Console.WriteLine("Directory is Last Accessed on " + dir.LastAccessTime);
                }
                else
                {
                    dir.Create();
                    Console.WriteLine(path + "Directory is created successfully");
                }
                //Delete this directory
                Console.WriteLine("If you want to delete this directory press small y. Press any key to exit.");
                try
                {
                    char ch = Convert.ToChar(Console.ReadLine());
                    if (ch == 'y')
                    {
                        if (dir.Exists)
                        {
                            dir.Delete();
                            Console.WriteLine(path + "Directory Deleted");
                        }
                        else
                        {
                            Console.WriteLine(path + "Directory Not Exists");
                        }
                    }                    
                }
                catch
                {
                    Console.WriteLine("Press Enter to Exit");
                }
                Console.ReadKey();
            }
            catch(DirectoryNotFoundException d)
            {
                Console.WriteLine("Exception raised : " + d.Message);
            }
        }
    }
}

Output
D:\csharp1Directory is created successfully
If you want to delete this directory press small y. Press any key to exit. _

Summary

In this chapter you learned DirectoryInfo class and their programming uses. The next chapter is FileInfo class which will teaches you how to use it in c# programming.

 

Share your thought