C# merge one directory with another

前端 未结 2 620
[愿得一人]
[愿得一人] 2021-01-18 04:45

I have an autoupdater C# program. It will download a rar file that holds the changed or new files for the update to some software. The rar file has it\'s structure just like

相关标签:
2条回答
  • 2021-01-18 05:21

    DirectoryInfo Class

    The following example demonstrates how to copy a directory and its contents.

    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's 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);
        }
    }
    
    0 讨论(0)
  • 2021-01-18 05:29

    The simplest way is to use the FileSystem.MoveDirectory method. Make sure you add a reference to the Microsoft.VisualBasic.dll:

    using Microsoft.VisualBasic.FileIO;
    ...
    // Merge D:\SourceDir with D:\DestDir:
    FileSystem.MoveDirectory("D:\\SourceDir", "D:\\DestDir", true /* Overwrite */);
    
    0 讨论(0)
提交回复
热议问题