Saturday, February 11, 2012

Working with Directory Class in .Net (C#)



Directory class is static class provides static methods to do normal directory related operations such as Copy, Paste, Move, Rename, Delete etcIt is available in System.IO namespace.  Below code demonstrates some basic majorly used methods of directory class.


//Creates temp directory if not exist
if (!Directory.Exists(@"d:\temp"))
    Directory.CreateDirectory(@"d:\temp");

//set/get current directory
Directory.SetCurrentDirectory(@"d:\temp");
Console.WriteLine(Directory.GetCurrentDirectory());

//gets root of directory
Console.WriteLine(Directory.GetDirectoryRoot(@"d:\temp"));

//gets creation and last modified time of directory
Console.WriteLine(Directory.GetCreationTime(@"d:\temp"));
Console.WriteLine(Directory.GetLastWriteTime(@"d:\temp"));
           
//enumerates all files available in temp directory          
foreach (string file in Directory.EnumerateFiles(@"d:\temp"))
    Console.WriteLine(file);

//enumerates all directories available in temp directory
foreach (string directory in Directory.EnumerateDirectories(@"d:\temp"))
    Console.WriteLine(directory);

//enumerates all logical drives
foreach (string drive in Directory.GetLogicalDrives())
    Console.WriteLine(drive);

//deletes an empty directory. if not empty throws an exception
Directory.Delete(@"d:\temp");


Enumerate files and folders using DirectoryInfo
DirectoryInfo class provides some basic features to create, copy, move, rename and delete operations. It also provides some basic information like directory name, full path, attribute, root name etc. Let’s have a look on below code which loop through all the files and directories in specific directory.


DirectoryInfo di = new DirectoryInfo(@"d:\temp");
Console.WriteLine(di.FullName);

//gets all files in temp directory
foreach (FileInfo file in di.GetFiles())
    Console.WriteLine(file.Name);
//gets all directories in temp directory
foreach (DirectoryInfo dir in di.GetDirectories())
    Console.WriteLine(dir.Name);


How to get Special Folders?
Special folders are defined under Environment.SpecialFolder enum. This enum is available in System.Environment class. Let’s see how to get path for special folders like MyDocuments, MyPictures, Desktop, ProgramFiles, System, Windows etc.

Console.WriteLine("My Documents - {0}", Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
Console.WriteLine("My Videos - {0}", Environment.GetFolderPath(Environment.SpecialFolder.MyVideos));
Console.WriteLine("Windows - {0}", Environment.GetFolderPath(Environment.SpecialFolder.Windows));
Console.WriteLine("ProgramFiles - {0}", Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles));
Console.WriteLine("System - {0}", Environment.GetFolderPath(Environment.SpecialFolder.System));

Output –
My Documents - D:\Users\mitesh\Documents
My Videos - D:\Users\mitesh\Videos
Windows - C:\WINDOWS
ProgramFiles - C:\Program Files
System - C:\WINDOWS\system32

//Enumerates all SpecialFolders available in Enviroment.SpecialFolder enum
foreach (var specialFolder in Enum.GetValues(typeof(Environment.SpecialFolder)))
    Console.WriteLine(specialFolder);

Output –
Desktop
Programs
MyDocuments
Favorites
Startup
Recent
SendTo
StartMenu

No comments:

Post a Comment