c#全フォルダとファイルコードのコピーを実現

1079 ワード



private void Copy()

{

    System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo("D:\\data");
    CopyDirectorysAndFiles("c:\\", dir);

}

private void CopyDirectorysAndFiles(string dest, System.IO.DirectoryInfo srcdir)
{
    if (dest.LastIndexOf('\\') != (dest.Length - 1))
    {
        dest += "\\";
    }
    string destPath = dest + srcdir.Name + "\\";
    if (!System.IO.Directory.Exists(destPath))
    {
        System.IO.Directory.CreateDirectory(destPath);
    }

    System.IO.FileInfo[] files = srcdir.GetFiles();
    foreach (System.IO.FileInfo file in files)
    {
        file.CopyTo(destPath+file.Name , true);
    }

    System.IO.DirectoryInfo[] dirs = srcdir.GetDirectories();
    foreach (System.IO.DirectoryInfo dirInfo in dirs)
    {
        CopyDirectorysAndFiles(destPath , dirInfo);
    }
}