Wednesday, January 30, 2013

C# Create Delete Move Copy Rename Folder

Folder Manipulation (Create Delete Move Copy Rename Folder) in C# Sample Code:
 

//include this
using System.IO;

public void CreateFolder(string FolderPath)
{
    // Specify the directories you want to manipulate.
    DirectoryInfo di = new DirectoryInfo(FolderPath);
    try
    {
        //check folder already exists.
        if (di.Exists)
        {
            MessageBox.Show("Folder already exists.");

        }
        else 
        {
            //create folder
            di.Create();
        }  
    }
    catch (Exception e)
    {
        MessageBox.Show(e.ToString());
    }
}

public void DeleteFolder(string FolderPath)
{
    // Specify the directories you want to manipulate.
    DirectoryInfo di = new DirectoryInfo(FolderPath);
    try
    {
        //check folder exists.
        if (di.Exists)
        {
            //delete all including sub folder
            di.Delete(true);
        }
    }
    catch (Exception e)
    {
        MessageBox.Show(e.ToString());
    }
}

public void CopyAll(DirectoryInfo source, DirectoryInfo target)
{
    if (source.FullName.ToLower() == target.FullName.ToLower())
    {
        return;
    }

    // check target folder exists 
    if (Directory.Exists(target.FullName) == false)
    {
        //create target target folder
        Directory.CreateDirectory(target.FullName);
    }

    //copy each file to target folder
    foreach (FileInfo fi in source.GetFiles())
    {
        fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
    }

    //copy each sub folder to target folder
    foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
    {
        DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name);
        //copy all file from subfolder to target subfolder
        CopyAll(diSourceSubDir, nextTargetSubDir);
    }
}

public void MoveAll(DirectoryInfo source, DirectoryInfo target)
{
    try
    {
        //check source folder exist
        if(source.Exists)
            //check destination not exist
            if(!target.Exists)
                source.MoveTo(target.ToString());
    }
    catch (IOException e)
    {
        MessageBox.Show(e.Message);
    }
}

public void RenameTo(DirectoryInfo di, string NewName)
{
    if (di == null)
    {
        MessageBox.Show("Directory info to rename cannot be null");
    }

    if (string.IsNullOrWhiteSpace(NewName))
    {
        MessageBox.Show("New name cannot be null or blank");
    }

    di.MoveTo(Path.Combine(di.Parent.FullName, NewName));
}

//method usage
CreateFolder(@"C:\NewFolder");
DeleteFolder(@"C:\FolderToDelete");

DirectoryInfo Source = new DirectoryInfo(@"C:\SourceFolder");
DirectoryInfo Target = new DirectoryInfo(@"C:\TargetFolder");
CopyAll(Source, Target);
MoveAll(Source, Target);

RenameTo(Source, "NewName");


Additional Reading :
  1. MSDN Directory Info
  2. Move Folder Example
  3. Renaming Folder Example

No comments:

Post a Comment