Wednesday, August 28, 2013

C# - Working with File System Objects(Part 2)

This article is the second from the “Working with File System Objects” series (the first article covered the “Directory” class), this article will cover the “DirectortInfo” class that located under the system.Io namespace.

DirectoryInfo Class
Not like the “Directory” class that expose only static methods the DirectoryInfo class exposed only instances. Therefore you must initialize a new class object before you can use it.

Note!
To use this class please add the “System.IO” namespace.

Class Summary:
This table contain the main methods you can use while working with this class.

Method Name
Why to use?
Instance.Create();
When you need to Create folder
Instance.Attributes
When you need to verify the object type (Folder or File)
Instance.Parent
When you need to get the parent folder
Instance.CreationTime
When you need to extract the folder creation time
Instance.FullName
When you need to get the folder full path
Instance.Name
When you need to get a specific folder name
Instance.Root
When you need the folder partition
Instance.Exists;
When you need to check if folder existing

 

Code Examples:

Create Folder


This example create a new folder, all you need is to pay attention that you need to add the new folder path when initialize the new class instance.

static void Main(string[] args)
        {
            DirectoryInfo a = new DirectoryInfo("c:\\New Folder"); 
            a.Create();
        }

Validate Folder existence

This example will verified if folder existing on your File System tree

static void Main(string[] args)
        {
            DirectoryInfo a = new DirectoryInfo("c:\\New Folder");
            Console.WriteLine(a.Exists); //True OR False
        }

Folder creation time

This example will return the folder creation date

static void Main(string[] args)
        {
            DirectoryInfo a = new DirectoryInfo("c:\\New Folder");
            Console.WriteLine(a.CreationTime);
        }

Receiving the folder full path

This example will return the folder full path

static void Main(string[] args)
        {
            DirectoryInfo a = new DirectoryInfo("c:\\New Folder");
            Console.WriteLine(a.FullName);
        }

Returning the folder name

This property will return the folder name and not the full path.

static void Main(string[] args)
        {
            DirectoryInfo a = new DirectoryInfo("c:\\New Folder");
            Console.WriteLine(a.Name);
        }

Returning the root partition

This prop will return the folder partition for a given folder

static void Main(string[] args)
        {
            DirectoryInfo a = new DirectoryInfo("c:\\New Folder");
            Console.WriteLine(a.Root);
        }

Validate the object type

This prop will be used when you want to validate if the object is file or folder

static void Main(string[] args)
        {
            DirectoryInfo a = new DirectoryInfo("c:\\New Folder");
            Console.WriteLine(a.Attributes);
        }

No comments:

Post a Comment

My Presentations