Monday, May 24, 2021

C# DirectoryInfo class

DirectoryInfo is a sealed class in System.IO namespace. This class has non-static methods and properties. The methods can be used to create, delete, or move a folder, The properties of the DirectotyInfo can be used to get information about the directory creation time, its name and full name etc. As its methods and properties are non-static, we therefore, create an instance of DirectoryInfo to acccess these methods and properties.
Example

  using System;
using System.Collections.Generic;
using System.IO;

namespace ConsoleDirectoryInfo
{
    class Program
    {
        static void Main(string[] args)
        {
            //DirectoryInfo class has non static methods 
            //so, we create an instance of DirectoryInfo class
            string strPath = @"F:\My folder";// @ for verbatim literal, escape character becomes normal character, it is no more metacharacter
            DirectoryInfo dir = new DirectoryInfo(strPath);
            dir.Create();
            Console.WriteLine("Folder created...");
            dir.CreateSubdirectory("Child Folder");
            Console.WriteLine("Subfolder created...");
            Console.WriteLine("Folder Creation Time: ",dir.CreationTime);
            DirectoryInfo dir2 = new DirectoryInfo(@"F:\");
            Console.WriteLine("\nEnumeration Approach:List Folders-");
            IEnumerable dirs = dir2.EnumerateDirectories();
            foreach (var d in dirs)
            {
                Console.WriteLine(d.FullName);
            }
            DirectoryInfo[] folders = dir2.GetDirectories();
            Console.WriteLine("\nArray Approach:List Folders-");
            foreach (var folder in folders)
            {
                Console.WriteLine(folder.FullName);

            }
            using (File.Create(strPath + "\\Test1.txt"))
            {
                //close the resource after file creation
                //without using block, dir.Delete(true) throws exception
            }
            //dir.Delete(true);//force to delete non empty folder
            string path2 = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
            DirectoryInfo di = new DirectoryInfo(path2);
            FileInfo[] files = di.GetFiles();
            Console.WriteLine("\nPictures List:-");
            foreach (var file in files)
            {
                Console.WriteLine(file.Name);
            }

            //Create a directory and move directory and its all contents
            try
            {
                dir.MoveTo(@"F:\All Data");
                Console.WriteLine("\nFolder moved...");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        //Properties of directory
            Console.WriteLine(dir.Attributes);
            Console.WriteLine(dir.CreationTime);
            Console.WriteLine(dir.FullName);
            Console.WriteLine(dir.Name);
            Console.WriteLine(dir.LastAccessTime);
            Console.ReadKey();
        }
    }
}

No comments:

Post a Comment

Hot Topics