Directory Class
1. Deals with Directories and Sub-Directories.
2. Using System.IO
3. Static in Nature.
4. No Instantiation Required.
5. It Cannot be Inherited.
3. Directory Class
1. Deals with Directories and Sub-Directories.
2. Using System.IO
3. Static in Nature.
4. No Instantiation Required.
5. It Cannot be Inherited.
4. Methods:
Name Description
CreateDirectory(String) Creates all directories and subdirectories in the specified
path unless they already exist.
CreateDirectory(String,
DirectorySecurity)
Creates all the directories in the specified path, unless
the already exist, applying the specified Windows
security.
Delete(String) Deletes an empty directory from a specified path.
Delete(String, Boolean) Deletes the specified directory and, if indicated, any
subdirectories and files in the directory.
EnumerateDirectories(String) Returns an enumerable collection of directory names in a
specified path.
5. Exists Determines whether the given path refers to an existing
directory on disk.
Move Moves a file or a directory and its contents to a new
location.
GetCurrentDirectory Gets the current working directory of the application.
SetCurrentDirectory Sets the application's current working directory to the
specified directory.
GetLastAccessTime Returns the date and time the specified file or directory
was last accessed.
6. Remarks:
Efficient to perform One directory-related action.
The static methods of the Directory class perform security
checks on all methods.
Make Use of DirectoryInfo instance methods if we want to
reuse objects several times.
The security check will not always be necessary.
7. using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
namespace DirectoryExample
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("n**************************************************
*******n");
Console.WriteLine("tt DIRECTORY EXAMPLE:");
Console.WriteLine("n**************************************************
*******n");
if (Directory.Exists("c:testDir1"))
{
//Shows Message If testdir1 Exists
Console.WriteLine("t Directory 'testDir' Exist ");
}
8. else
{
// Create The Directory testDir1
Directory.CreateDirectory("C:TestDir1");
Console.WriteLine("t Directory: TestDir1 Created....!!n");
// Create The Directory testDir2
Directory.CreateDirectory("C:TestDir1TestDir2");
Console.WriteLine("t Directory: TestDir2 Created....!!n");
// Move The Directory testDir2 As testDir in c:
Directory.Move("C:TestDir1TestDir2", "C:TestDir");
Console.WriteLine("t Directory: TestDir2 Moved....!!n");
// Delete The Directory testDir1
Directory.Delete("C:TestDir1", true);
Console.WriteLine("t Directory: TestDir1 Deleted....!!n");
}
Console.WriteLine("n***************************************************
******n");
Console.ReadKey();
}
}
}