【问题标题】:Copy file to a different directory将文件复制到其他目录
【发布时间】:2011-11-19 18:01:33
【问题描述】:

我正在做一个项目,我想将一个目录中的一些文件复制到另一个已经存在的目录。

我找不到简单地从一个文件夹复制到另一个文件夹的方法。我可以找到将文件复制到新文件,或将目录复制到新目录。

我现在设置程序的方式是复制文件并将其保留在同一目录中,然后将该副本移动到我想要的目录。

编辑:

谢谢大家。你所有的答案都有效。我意识到我做错了什么,当我设置目标路径时我没有添加文件名。现在一切正常,感谢您的超快速响应。

【问题讨论】:

  • 你应该接受一个答案...

标签: c# .net file


【解决方案1】:

NET6 - 扩展方法

[DebuggerStepThrough]
public static FileInfo CopyToDir(this FileInfo srcFile, string destDir) =>
    (srcFile == null || !srcFile.Exists) ? throw new ArgumentException($"The specified source file [{srcFile.FullName}] does not exist", nameof(srcFile)) :
    (destDir == null || !Directory.Exists(destDir)) ? throw new ArgumentException($"The specified destination directory [{destDir}] does not exist", nameof(destDir)) :
    srcFile.CopyTo(Path.Combine(destDir, srcFile.Name), true);

【讨论】:

    【解决方案2】:

    如果目标目录不存在,File.Copy 将抛出。这个版本解决了这个问题

    public void Copy(
                string sourceFilePath,
                string destinationFilePath,
                string destinationFileName = null)
    {
           if (string.IsNullOrWhiteSpace(sourceFilePath))
                    throw new ArgumentException("sourceFilePath cannot be null or whitespace.", nameof(sourceFilePath));
           
           if (string.IsNullOrWhiteSpace(destinationFilePath))
                    throw new ArgumentException("destinationFilePath cannot be null or whitespace.", nameof(destinationFilePath));
           
           var targetDirectoryInfo = new DirectoryInfo(destinationFilePath);
    
           //this creates all the sub directories too
           if (!targetDirectoryInfo.Exists)
               targetDirectoryInfo.Create();
    
           var fileName = string.IsNullOrWhiteSpace(destinationFileName)
               ? Path.GetFileName(sourceFilePath)
               : destinationFileName;
    
           File.Copy(sourceFilePath, Path.Combine(destinationFilePath, fileName));
    }
    

    在 .NET Core 2.1 上测试

    【讨论】:

      【解决方案3】:

      我使用了这段代码,它对我有用

      //I declare first my variables
      string sourcePath = @"Z:\SourceLocation";
      string targetPath = @"Z:\TargetLocation";
      
      string destFile = Path.Combine(targetPath, fileName);
      string sourceFile = Path.Combine(sourcePath, fileName);
      
      // To copy a folder's contents to a new location:
      // Create a new target folder, if necessary.
      if (!Directory.Exists(targetPath))
      {
          Directory.CreateDirectory(targetPath);
      }
      
      // To copy a file to another location and 
      // overwrite the destination file if it already exists.
      File.Copy(sourceFile, destFile, true);
      

      【讨论】:

        【解决方案4】:

        这对我有用:

            string picturesFile = @"D:\pictures";
            string destFile = @"C:\Temp\tempFolder\";
        
            string[] files = Directory.GetFiles(picturesFile);
            foreach (var item in files)
            {
               File.Copy(item, destFile + Path.GetFileName(item));
            }
        

        【讨论】:

          【解决方案5】:
          string fileToCopy = "c:\\myFolder\\myFile.txt";
          string destinationDirectory = "c:\\myDestinationFolder\\";
          
          File.Copy(fileToCopy, destinationDirectory + Path.GetFileName(fileToCopy));
          

          【讨论】:

          • 我宁愿使用Path.Combine 来连接路径字符串。
          【解决方案6】:
          File.Copy(@"someDirectory\someFile.txt", @"otherDirectory\someFile.txt");
          

          工作正常。

          【讨论】:

            【解决方案7】:

            也许

            File.Copy("c:\\myFolder\\myFile.txt", "c:\\NewFolder\\myFile.txt");
            

            ?

            【讨论】:

            • 这不会将文件从一个目录复制到另一个目录,这就是问题所在。
            • @svick 你的回答和我的有什么不同?
            • 不是现在,而是在您进行编辑之前(查看您的回答历史记录)。
            【解决方案8】:

            MSDN File.Copy

            var fileName = "sourceFile.txt";
            var source = Path.Combine(Environment.CurrentDirectory, fileName);
            var destination = Path.Combine(destinationFolder, fileName);
            
            File.Copy(source, destination);
            
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-10-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-10-09
            • 2012-10-12
            相关资源
            最近更新 更多