C#中的文件夹副本

时间:2009-12-29 10:52:18

标签: c#

我的机器中有一个包含10个文本文件的文件夹,位于C:\ TEXTFILES \ drive。我想将文件夹TEXTFILES及其内容从我的机器完全复制到另一台机器。如何使用C#复制它。

5 个答案:

答案 0 :(得分:34)

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);
            }
        }
    }
}

来自MSDN

答案 1 :(得分:9)

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));
    }

}

答案 2 :(得分:2)

        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");
        }

有关更深层次的副本,请参阅:

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

答案 3 :(得分:1)

  1. 分享您的目标文件夹。
  2. 有很多方法可以做到这一点。见如下:
    Copy Folders in C# using System.IO
    Copy the entire contents of a directory in C#

答案 4 :(得分:-4)

您可以在System.IO命名空间中找到所需的一切,特别是FileDirectory类。