Copy Directory : Directory : File Stream C# Source Code


Custom Search

C# Source Code » File Stream » Directory »

 

Copy Directory








    
 
using System;
using System.IO;

class MainClass {
    static void Main(string[] args) {
        DirectoryInfo sourceDir = new DirectoryInfo("c:\\source");
        DirectoryInfo destinationDir = new DirectoryInfo("c:\\target");

        CopyDirectory(sourceDir, destinationDir);

    }

    static void CopyDirectory(DirectoryInfo source, DirectoryInfo destination) {
        if (!destination.Exists) {
            destination.Create();
        }
        // Copy all files.
        FileInfo[] files = source.GetFiles();
        foreach (FileInfo file in files) {
            file.CopyTo(Path.Combine(destination.FullName,
               file.Name));
        }

        // Process subdirectories.
        DirectoryInfo[] dirs = source.GetDirectories();
        foreach (DirectoryInfo dir in dirs) {
            string destinationDir = Path.Combine(destination.FullName, dir.Name);
            CopyDirectory(dir, new DirectoryInfo(destinationDir));
        }
    }
}

 
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo File Stream
» Directory