“Folders” Handling

• The System.IO namespace necessary API to access the folders information also.
• According to this, a folder is to be represented as an object.
• The folder object is able to get the details of the folder like folder full path, sub directories and files etc.


API: System.IO.DirectoryInfo

This class object represents a folder on the file system. This able to get the information of the folder and also to perform certain operations on that folder.

Object Construction:
Syn: DirectoryInfo obj = new DirectoryInfo(“path of the directory”);
Ex: DirectoryInfo obj = new DirectoryInfo(“c:\\windows”);
You can observe the list of all available properties, methods of this class.
Exists Checks the existence of the directory. If it exists, it indicates “true”. Otherwise, it indicates “false”.
Name Gets only the name of the directory (without path)
FullName Gets the name along with full path.
CreationTime Gets the date & time, when the directory is created.
LastAccessTime Gets the date & time, when the directory is accessed last time.
LastWriteTime Gets the date & time, when the directory is modified last time.
Parent Gets the directory object, which represents the respective parent folder.
Root Gets the name of the drive, in which the directory exists.
Create() Creates the a new directory.
CreateSubdirectory(name) Creates a sub directory.
Delete() Deletes the directory, if it is empty.
Delete(true) Deletes the entire directory, along with it’s sub directories and files.
GetFiles() Gets all the files in the directory as “FileInfo” class object array.
GetFiles(“search pattern”) Gets the files in the directory as “FileInfo” class object array, based on the given search pattern.
GetDirectories() Gets all the directories in the directory as “DirectoryInfo” class object array.
GetDirectories(“search pattern”) Gets the directories in the directory as “DirectoryInfo” class object array, based on the given search pattern.

Demo on “DirectoryInfo” class

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

namespace DirectoryInfoDemo1
{
class Program
{
static void Main(string[] args)
{
string directorypath = "c:\\windows\\help";
DirectoryInfo dinfo = new DirectoryInfo(directorypath);
if (dinfo.Exists)
{
Console.WriteLine("Directory Name: " + dinfo.Name);
Console.WriteLine("Directory Full Path: " + dinfo.FullName);
Console.WriteLine("Created on: " + dinfo.CreationTime);
Console.WriteLine("Last accessed on: " + dinfo.LastAccessTime);
Console.WriteLine("Last modified on: " + dinfo.LastWriteTime);
Console.WriteLine("Parent: " + dinfo.Parent.FullName);
Console.WriteLine("Root: " + dinfo.Root);

Console.WriteLine("\nFiles:");
FileInfo[] fobjs = dinfo.GetFiles();
foreach (FileInfo f in fobjs)
Console.WriteLine(f.FullName)
;
Console.WriteLine("\nDirectories:");
DirectoryInfo[] dobjs = dinfo.GetDirectories();
foreach (DirectoryInfo d in dobjs)
Console.WriteLine(d.FullName);
}
else
Console.WriteLine(directorypath + " is not available on the system.");
Console.Read();
}
}
}
 folder handling

Create the Directory

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

namespace DirectoryInfoDemo2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the directory path:");
string directorypath = Console.ReadLine();
DirectoryInfo dinfo = new DirectoryInfo(directorypath);
if (!dinfo.Exists)
{
dinfo.Create();
Console.WriteLine("Directory created.");
}
else
Console.WriteLine(directorypath + " is already exists.");
Console.Read();
}
}
}
 folder handling

Delete the Directory

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

namespace DirectoryInfoDemo3
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the directory path:");
string directorypath = Console.ReadLine();
DirectoryInfo dinfo = new DirectoryInfo(directorypath);
if (dinfo.Exists)
{
dinfo.Delete(true);
Console.WriteLine("Directory deleted.");
}
else
Console.WriteLine(directorypath + " doesn't exists.");
Console.Read();
}
}
}
 folder handling