开发者

Folder copy in C# [duplicate]

开发者 https://www.devze.com 2022-12-14 20:58 出处:网络
This question already has answers here: Copy the entire contents of a directory in C# (27 answers) Closed 6 years ago.
This question already has answers here: Copy the entire contents of a directory in C# (27 answers) Closed 6 years ago.

I have a folder with 10 text files at C:\TEXTFILES\ drive in my machine. 开发者_JAVA百科I want to copy the folder TEXTFILES and its contents completely from my machine to another machine. How to copy the same using C#.


using System;
using System.IO;

class DirectoryCopyExample
{
    static void Main()
    {
        DirectoryCopy(".", @".\temp", true);
    }

    private static void DirectoryCopy(
        string sourceDirName, string destDirName, bool copySubDirs)
    {
      DirectoryInfo dir = new DirectoryInfo(sourceDirName);
      DirectoryInfo[] dirs = dir.GetDirectories();

      // If the source directory does not exist, throw an exception.
        if (!dir.Exists)
        {
            throw new DirectoryNotFoundException(
                "Source directory does not exist or could not be found: "
                + sourceDirName);
        }

        // If the destination directory does not exist, create it.
        if (!Directory.Exists(destDirName))
        {
            Directory.CreateDirectory(destDirName);
        }


        // Get the file contents of the directory to copy.
        FileInfo[] files = dir.GetFiles();

        foreach (FileInfo file in files)
        {
            // Create the path to the new copy of the file.
            string temppath = Path.Combine(destDirName, file.Name);

            // Copy the file.
            file.CopyTo(temppath, false);
        }

        // If copySubDirs is true, copy the subdirectories.
        if (copySubDirs)
        {

            foreach (DirectoryInfo subdir in dirs)
            {
                // Create the subdirectory.
                string temppath = Path.Combine(destDirName, subdir.Name);

                // Copy the subdirectories.
                DirectoryCopy(subdir.FullName, temppath, copySubDirs);
            }
        }
    }
}

From MSDN


private void copyDirectory(string strSource, string strDestination)
{
    if (!Directory.Exists(strDestination))
    {
        Directory.CreateDirectory(strDestination);
    }

    DirectoryInfo dirInfo = new DirectoryInfo(strSource);
    FileInfo[] files = dirInfo.GetFiles();
    foreach(FileInfo tempfile in files )
    {
        tempfile.CopyTo(Path.Combine(strDestination,tempfile.Name));
    }

    DirectoryInfo[] directories = dirInfo.GetDirectories();
    foreach(DirectoryInfo tempdir in directories)
    {
        copyDirectory(Path.Combine(strSource, tempdir.Name), Path.Combine(strDestination, tempdir.Name));
    }

}


        string path = @"C:\TEXTFILES\";
        string path2 = @"P:\myNetworkPath\tesssst";

        try
        {
            Directory.CreateDirectory(path2);

            foreach (string fileName in Directory.GetFiles(path))
            {
                File.Copy(
                    Path.Combine(path, fileName),
                    Path.Combine(path2, fileName), true);
            }
        }
        catch
        {
            Console.WriteLine("Exception");
        }

For a deeper copy, see:

http://www.codeproject.com/KB/files/copydirectoriesrecursive.aspx


  1. Share your destination folder.
  2. There are a lot of ways to do this. See followings:
    Copy Folders in C# using System.IO
    Copy the entire contents of a directory in C#
0

精彩评论

暂无评论...
验证码 换一张
取 消