Copy Folders in C# using System.IO

后端 未结 9 824
傲寒
傲寒 2020-12-10 03:20

I need to Copy folder C:\\FromFolder to C:\\ToFolder

Below is code that will CUT my FromFolder and then will create my ToFolder. So my FromFolder will be gone and al

相关标签:
9条回答
  • 2020-12-10 03:53

    Copying directories (correctly) is actually a rather complex task especially if you take into account advanced filesystem techniques like junctions and hard links. Your best bet is to use an API that supports it. If you aren't afraid of a little P/Invoke, SHFileOperation in shell32 is your best bet. Another alternative would be to use the Microsoft.VisualBasic.FileIO.FileSystem.CopyDirectory method in the Microsoft.VisualBasic assembly (even if you aren't using VB).

    0 讨论(0)
  • 2020-12-10 04:02

    This article provides an alogirthm to copy recursively some folder and all its content

    From the article :

    Sadly there is no built-in function in System.IO that will copy a folder and its contents. Following is a simple recursive algorithm that copies a folder, its sub-folders and files, creating the destination folder if needed. For simplicity, there is no error handling; an exception will throw if anything goes wrong, such as null or invalid paths or if the destination files already exist.

    Good luck!

    0 讨论(0)
  • 2020-12-10 04:03

    My version of DirectoryInfo.CopyTo using extension.

    public static class DirectoryInfoEx {
        public static void CopyTo(this DirectoryInfo source, DirectoryInfo target) {
            if (source.FullName.ToLower() == target.FullName.ToLower())
                return;
    
            if (!target.Exists)
                target.Create();
    
            foreach (FileInfo f in source.GetFiles()) {
                FileInfo newFile = new FileInfo(Path.Combine(target.FullName, f.Name));
                f.CopyTo(newFile.FullName, true);
            }
    
            foreach (DirectoryInfo diSourceSubDir in source.GetDirectories()) {
                DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name);
                diSourceSubDir.CopyTo(nextTargetSubDir);
            }
        }
    }
    

    And use like that...

    DirectoryInfo d = new DirectoryInfo("C:\Docs");
    d.CopyTo(new DirectoryInfo("C:\New"));
    
    0 讨论(0)
  • 2020-12-10 04:06

    You'll need to create a new directory from scratch then loop through all the files in the source directory and copy them over.

    string[] files = Directory.GetFiles(GlobalVariables.mstrReadsWellinPath);
    foreach(string s in files)
    {
            fileName=Path.GetFileName(s);
            destFile = Path.Combine(DestinationPath, fileName);
            File.Copy(s, destFile);
    }
    

    I leave creating the destination directory to you :-)

    0 讨论(0)
  • 2020-12-10 04:07

    there is a file copy. Recreate folder and copy all the files from original directory to the new one example

    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.Combine(destination.FullName, 
                    file.Name));
            }
    
            // Process subdirectories.
            DirectoryInfo[] dirs = source.GetDirectories();
            foreach (DirectoryInfo dir in dirs)
            {
                // Get destination directory.
                string destinationDir = Path.Combine(destination.FullName, dir.Name);
    
                // Call CopyDirectory() recursively.
                CopyDirectory(dir, new DirectoryInfo(destinationDir));
            }
        }
    
    0 讨论(0)
  • 2020-12-10 04:07

    You're right. There is no Directory.Copy method. It would be a very powerful method, but also a dangerous one, for the unsuspecting developer. Copying a folder can potentionaly be a very time consuming operation, while moving one (on the same drive) is not.

    I guess Microsoft thought it would make sence to copy file by file, so you can then show some kind of progress information. You could iterate trough the files in a directory by creating an instance of DirectoryInfo and then calling GetFiles(). To also include subdirectories you can also call GetDirectories() and enumerate trough these with a recursive method.

    0 讨论(0)
提交回复
热议问题