Move and copy Directory : Dictionary « Collections Data Structure « C# / C Sharp






Move and copy Directory

       
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Td.Additional.IO
{
    /// <summary>
    /// Directory facilities.
    /// </summary>
    public static class Directory
    {
        /// <summary>
        /// Moves the directory.
        /// </summary>
        /// <param name="sourceDirectory">The source directory.</param>
        /// <param name="destinationDirectory">The destination directory.</param>
        /// <param name="deleteSource">if set to <c>true</c> [delete source].</param>
        public static void MoveDirectory(
            string sourceDirectory,
            string destinationDirectory,
            bool deleteSource)
        {
            if (System.IO.Directory.Exists(sourceDirectory))
            {
                if (System.IO.Directory.GetDirectoryRoot(sourceDirectory) ==
                    System.IO.Directory.GetDirectoryRoot(destinationDirectory))
                {
                    System.IO.Directory.Move(sourceDirectory, destinationDirectory);
                }
                else
                {
                    try
                    {
                        CopyDirectory(new DirectoryInfo(sourceDirectory),
                            new DirectoryInfo(destinationDirectory));
                        if (deleteSource)
                            System.IO.Directory.Delete(sourceDirectory, true);
                    }
                    catch (Exception subEx)
                    {
                        throw subEx;
                    }
                }
            }
        }

        /// <summary>
        /// Copies the directory.
        /// </summary>
        /// <param name="sourceDirectory">The source directory.</param>
        /// <param name="destinationDirectory">The destination directory.</param>
        public static void CopyDirectory(
            DirectoryInfo sourceDirectory,
            DirectoryInfo destinationDirectory)
        {
            if (!destinationDirectory.Exists)
                destinationDirectory.Create();
            FileInfo[] fiSrcFiles = sourceDirectory.GetFiles();
            foreach (FileInfo fiSrcFile in fiSrcFiles)
                fiSrcFile.CopyTo(Path.Combine(destinationDirectory.FullName, fiSrcFile.Name));
            DirectoryInfo[] diSrcDirectories = sourceDirectory.GetDirectories();

            foreach (DirectoryInfo diSrcDirectory in diSrcDirectories)
                CopyDirectory(
                    diSrcDirectory,
                    new DirectoryInfo(
                        Path.Combine(destinationDirectory.FullName, diSrcDirectory.Name)));
        }
    }
}

   
    
    
    
    
    
    
  








Related examples in the same category

1.Dictionary(TKey, TValue) represents a collection of keys and values.
2.Try to add duplicate entry to Dictionary
3.Change value for a key
4.The indexer throws an exception if the requested key is not in the dictionary.
5.Use TryGetValue to get a value out
6.ContainsKey can be used to test keys before inserting them
7.for each KeyValuePair
8.To get the values alone, use the Values property
9.To get the keys alone, use the Keys property.
10.Use the Remove method to remove a key/value pair.
11.Dictionary List
12.An Hashtable-backed dictionary that enumerates Keys and Values in insertion order.
13.Represents a dictionary which stores the values as weak references instead of strong references.
14.Dictionary Pretty Print
15.Clone a Dictionary
16.Scoped Dictionary
17.Merges two dictionaries.