【问题标题】:i have got this code of MSDN but still can't get my program to work with it我有这个 MSDN 代码,但仍然无法让我的程序使用它
【发布时间】:2011-06-21 12:11:03
【问题描述】:

我可以从多个目录复制所有文件,但我想要做的是复制所有目录以及其中的文件,因为它们是我从中复制的位置,而不仅仅是将文件放在我的目标文件夹中。到目前为止,这是我的代码

{
    string SelectedPath = (string)e.Argument;
    string sourceDirName;
    string destDirName;
    bool copySubDirs;
    DirectoryCopy(".", SelectedPath, true);

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

任何帮助将不胜感激

【问题讨论】:

    标签: c# file directory copy


    【解决方案1】:

    复制目录没有现成的方法。你能做的最好的就是使用扩展方法。看看这个 - http://channel9.msdn.com/Forums/TechOff/257490-How-Copy-directories-in-C/07964d767cc94c3990bb9dfa008a52c8

    这是完整的示例(刚刚测试过并且可以正常工作):

    using System;
    using System.IO;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                var di = new DirectoryInfo("C:\\SomeFolder");
                di.CopyTo("E:\\SomeFolder", true);
            }
        }
    
    public static class DirectoryInfoExtensions
    {
        // Copies all files from one directory to another.
        public static void CopyTo(this DirectoryInfo source, string destDirectory, bool recursive)
        {
            if (source == null)
                throw new ArgumentNullException("source");
            if (destDirectory == null)
                throw new ArgumentNullException("destDirectory");
    
            // If the source doesn't exist, we have to throw an exception.
            if (!source.Exists)
                throw new DirectoryNotFoundException("Source directory not found: " + source.FullName);
            // Compile the target.
            DirectoryInfo target = new DirectoryInfo(destDirectory);
            // If the target doesn't exist, we create it.
            if (!target.Exists)
                target.Create();
    
            // Get all files and copy them over.
            foreach (FileInfo file in source.GetFiles())
            {
                file.CopyTo(Path.Combine(target.FullName, file.Name), true);
            }
    
            // Return if no recursive call is required.
            if (!recursive)
                return;
    
            // Do the same for all sub directories.
            foreach (DirectoryInfo directory in source.GetDirectories())
            {
                CopyTo(directory, Path.Combine(target.FullName, directory.Name), recursive);
            }
        }
    }
    

    }

    【讨论】:

    • 我使用此代码时遇到错误,它说 system.IO.directoryinfo 不包含 copyto 的定义
    • 如何使用扩展方法请参考MSDN article
    【解决方案2】:

    这样做的聪明方法就像在 nithins 回答中一样。使用您的方法,您可以使用 FileInfo.Directory 信息来确定文件的来源,然后在需要时在您的目标中创建此目录。但是 nithins 链接是一种更清洁的解决方案。

    【讨论】:

      【解决方案3】:

      也许尝试在复制之前查看路径是否存在。如果不存在,则创建它?

      string folderPath = Path.GetDirectoryName(path);
      if (!Directory.Exists(folderPath))
          Directory.CreateDirectory(folderPath);
      

      【讨论】:

        【解决方案4】:

        【讨论】:

        • 是的,我已经尝试过,但由于某种原因它在我的代码中不起作用
        • 我不知道为什么我给出一个错误每段代码都只是给我一个错误。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-19
        • 2012-05-19
        • 1970-01-01
        相关资源
        最近更新 更多