CODEDIGEST
Home » CodeDigest
Search
 

Technologies
 

Directory Class Vs DirectoryInfo Class
Submitted By Satheesh Babu B
On 10/28/2008 9:59:39 PM
Tags: asp.net,C#,CodeDigest  

 

Difference

Directory class is a static class which can be used to create, move, enumerate directories and sub directories. The DirectoryInfo class is also served for the same purpose like Directory class where its members are instance members as opposed to Directory class. The main difference between the two lies in when we can use these classes. Directory class can be used when we want to a simple folder operation at once. For example, you need to delete the folder and get away. But, the DirectoryInfo class is associated with a folder and provides you all the operations that can be done on the folder. The DirectoryInfo class accepts a path as parameter when instantiating and provides you everything on the folder. You can create subdirectories, move, enumerate etc.

 

Directory Class Usage

        string root = Server.MapPath("~");

        string path = root + "\\test\\";

       //Create Directory

        Directory.CreateDirectory(path);

       //Delete Directory

        Directory.Delete(path);

 

If you see the above code, we need to give the path the folder every time we do an operation on a folder. Thus, we can use this for single operation on a folder.

 

DirectoryInfo Class Usage

string path = root + "\\test\\";

        DirectoryInfo dirinfo = new DirectoryInfo(path);

       //Create Directory

        dirinfo.Create();

       //Delete Directory

        dirinfo.Delete();

 

If you see the above code, we create a DirectoryInfo object with a folder and can perform series of operation on that folder like delete, creating sub directories, etc.

 

Do you have a working code that can be used by anyone? Submit it here. It may help someone in the community!!

Recent Codes
  • View All Codes..