Directory class in C#

In this article I am going g to explain about Directory class in c#.
  • 4946

Directory class

Directory class are use for many operation such as copy, moving, renaming, deleting and creating directory. Directory are also use for get for get Date time information about creation, access and writing directory.  In other word we can say that Directory Class mainly use for Create  directory, Delete Directory, Move Directory, Create file in Directory, Delete the file from the directory, count the the number of file and directory in the directory calculate the size of file and directory etc.

Directory method are static. The static method of directory class perform security check on all method. If you are going to reuse an object on several time consider using the corresponding  instance method DirectoryInfo instead. Because security check will not always be necessary . Dictionary method may be more effective if you want to perform only one action. Most of directory required path to the directory the you manipulating.

In members that accept a path, the path can refer to a file or just a directory. The specified path can also refer to a relative path or a Universal Naming Convention (UNC) path for a server and share name. For example, all the following are acceptable paths:

"c:\\Dire" ,  "Dire\\Subdire" ,  "\\\\MyServer\\MyShare"

By default full Read/Write access to new directory is granted to all user. Demanding permission for a directory where the path string ends with the directory separator character results in demanding permissions for all the contained subdirectories (for example, "C:\Temp\"). If permissions are required only for a specific directory, the string should end with a period (for example, "C:\Temp\.").

The following example defined If directory exist then delete it if not the create it. This example also defined move the directory, create a file in the directory and count the file in the directory.

using System;
using System.IO;

class Demo 
{
    public static void Main() 
    {
        // Specify the directories you want to manipulate.
        string path = @"c:\Dire";
        string Dest = @"c:\Dest";

        try 
        {
            // Determine whether the directory exists.
            if (!Directory.Exists(path)) 
            {
                // Create the directory it does not exist.
                Directory.CreateDirectory(path);
            }

            if (Directory.Exists(Dest)) 
            {
                // Delete the target to ensure it is not there.
                Directory.Delete(Dest, true);
            }

            // Move the directory.
            Directory.Move(path, Dest);

            // Create a file in the directory.
            File.CreateText(Dest + @"\testing.txt");

            // Count the files in the target directory.
            Console.WriteLine("The number of files in {0} is {1}",
                Dest, Directory.GetFiles(Dest).Length);
        } 
        catch (Exception e) 
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        } 
        finally {}
    }
}

 

The following example explain how to calculate the size of a Directory

 

using System;
using System.IO;

public class DireSize
{
    public static long DirSize(DirectoryInfo d) 
    {    
        long Size = 0;    
        // Add file sizes.
        FileInfo[] fis = d.GetFiles();
        foreach (FileInfo fi in fis) 
        {      
            Size += fi.Length;    
        }
        // Add subdirectory sizes.
        DirectoryInfo[] dis = d.GetDirectories();
        foreach (DirectoryInfo di in dis) 
        {
            Size += DirSize(di);   
        }
        return(Size);  
    }
    public static void Main(string[] args) 
    {
        if (args.Length != 1) 
        {
            Console.WriteLine("You must provide a directory argument at the command line.");    
        } 
        else 
        {  
            DirectoryInfo d = new DirectoryInfo(args[0]);
            long dsize = DirSize(d);
            Console.WriteLine("The size of {0} and its subdirectories is {1} bytes.", d, dsize);
        }
    }
}

Further Readings

You may read more about Directory class:  here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

© 2020 DotNetHeaven. All rights reserved.