Copy the entire contents of a directory in C#

前端 未结 22 826
日久生厌
日久生厌 2020-11-22 07:13

I want to copy the entire contents of a directory from one location to another in C#.

There doesn\'t appear to be a way to do this using System.IO class

相关标签:
22条回答
  • 2020-11-22 07:45

    Try this:

    Process proc = new Process();
    proc.StartInfo.UseShellExecute = true;
    proc.StartInfo.FileName = Path.Combine(Environment.SystemDirectory, "xcopy.exe");
    proc.StartInfo.Arguments = @"C:\source C:\destination /E /I";
    proc.Start();
    

    Your xcopy arguments may vary but you get the idea.

    0 讨论(0)
  • 2020-11-22 07:45

    This is my code hope this help

        private void KCOPY(string source, string destination)
        {
            if (IsFile(source))
            {
                string target = Path.Combine(destination, Path.GetFileName(source));
                File.Copy(source, target, true);
            }
            else
            {
                string fileName = Path.GetFileName(source);
                string target = System.IO.Path.Combine(destination, fileName);
                if (!System.IO.Directory.Exists(target))
                {
                    System.IO.Directory.CreateDirectory(target);
                }
    
                List<string> files = GetAllFileAndFolder(source);
    
                foreach (string file in files)
                {
                    KCOPY(file, target);
                }
            }
        }
    
        private List<string> GetAllFileAndFolder(string path)
        {
            List<string> allFile = new List<string>();
            foreach (string dir in Directory.GetDirectories(path))
            {
                allFile.Add(dir);
            }
            foreach (string file in Directory.GetFiles(path))
            {
                allFile.Add(file);
            }
    
            return allFile;
        }
        private bool IsFile(string path)
        {
            if ((File.GetAttributes(path) & FileAttributes.Directory) == FileAttributes.Directory)
            {
                return false;
            }
            return true;
        }
    
    0 讨论(0)
  • 2020-11-22 07:45

    tboswell 's replace Proof version (which is resilient to repeating pattern in filepath)

    public static void copyAll(string SourcePath , string DestinationPath )
    {
       //Now Create all of the directories
       foreach (string dirPath in Directory.GetDirectories(SourcePath, "*", SearchOption.AllDirectories))
          Directory.CreateDirectory(Path.Combine(DestinationPath ,dirPath.Remove(0, SourcePath.Length ))  );
    
       //Copy all the files & Replaces any files with the same name
       foreach (string newPath in Directory.GetFiles(SourcePath, "*.*",  SearchOption.AllDirectories))
          File.Copy(newPath, Path.Combine(DestinationPath , newPath.Remove(0, SourcePath.Length)) , true);
        }
    
    0 讨论(0)
  • 2020-11-22 07:46

    Copied from MSDN:

    using System;
    using System.IO;
    
    class CopyDir
    {
        public static void Copy(string sourceDirectory, string targetDirectory)
        {
            DirectoryInfo diSource = new DirectoryInfo(sourceDirectory);
            DirectoryInfo diTarget = new DirectoryInfo(targetDirectory);
    
            CopyAll(diSource, diTarget);
        }
    
        public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
        {
            Directory.CreateDirectory(target.FullName);
    
            // Copy each file into the new directory.
            foreach (FileInfo fi in source.GetFiles())
            {
                Console.WriteLine(@"Copying {0}\{1}", target.FullName, fi.Name);
                fi.CopyTo(Path.Combine(target.FullName, fi.Name), true);
            }
    
            // Copy each subdirectory using recursion.
            foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
            {
                DirectoryInfo nextTargetSubDir =
                    target.CreateSubdirectory(diSourceSubDir.Name);
                CopyAll(diSourceSubDir, nextTargetSubDir);
            }
        }
    
        public static void Main()
        {
            string sourceDirectory = @"c:\sourceDirectory";
            string targetDirectory = @"c:\targetDirectory";
    
            Copy(sourceDirectory, targetDirectory);
        }
    
        // Output will vary based on the contents of the source directory.
    }
    
    0 讨论(0)
提交回复
热议问题