Csharp/CSharp Tutorial/File Directory Stream/Directory Copy — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 15:31, 26 мая 2010

Copy Directory

using System;
using System.IO;
static class MainClass
{
    static void Main(string[] args)
    {
        DirectoryInfo sourceDir = new DirectoryInfo("c:\\a");
        DirectoryInfo destinationDir = new DirectoryInfo("c:\\b");
        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.rubine(destination.FullName, 
                file.Name));
        }
        // Process subdirectories.
        DirectoryInfo[] dirs = source.GetDirectories();
        foreach (DirectoryInfo dir in dirs)
        {
            // Get destination directory.
            string destinationDir = Path.rubine(destination.FullName, dir.Name);
            // Call CopyDirectory() recursively.
            CopyDirectory(dir, new DirectoryInfo(destinationDir));
        }
    }
}