C # merge one directory with another

I have an autoupdater C # program. It will download a rar file containing modified or new files for updating for some software. The rar file has a structure such as the base directory of the software, but contains only modified or new files / folders. Is there an easy way to “merge” these files / folders into a target directory, so that if the file / folder already exists, it will be replaced, and if it will not be added or I will have to manually move through each file / folder and do it on your own? Just hope there is a small merge function that has .NET :)

+5
source share
2 answers

Class DirectoryInfo

, .

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

    // Check if the target directory exists, if not, create it.
    if (Directory.Exists(target.FullName) == false)
    {
        Directory.CreateDirectory(target.FullName);
    }

    // Copy each file into it new directory.
    foreach (FileInfo fi in source.GetFiles())
    {
        Console.WriteLine(@"Copying {0}\{1}", target.FullName, fi.Name);
        fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
    }

    // Copy each subdirectory using recursion.
    foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
    {
        DirectoryInfo nextTargetSubDir =
            target.CreateSubdirectory(diSourceSubDir.Name);
        CopyAll(diSourceSubDir, nextTargetSubDir);
    }
}
+19

- FileSystem.MoveDirectory. , Microsoft.VisualBasic.dll:

using Microsoft.VisualBasic.FileIO;
...
// Merge D:\SourceDir with D:\DestDir:
FileSystem.MoveDirectory("D:\\SourceDir", "D:\\DestDir", true /* Overwrite */);
+1

All Articles