我正在处理一个项目,其中我希望将一个目录中的一些文件复制到另一个已经存在的目录中。
我找不到简单地从一个文件夹复制到另一个文件夹的方法。我可以找到将文件复制到新文件,或将目录复制到新目录。
我现在设置程序的方式是复制文件并将其放在相同的目录中,然后将副本移动到我想要的目录中。
编辑:
谢谢大家。你所有的答案都起作用了。我意识到我做错了什么,当我设置目标路径时,我没有添加文件名。现在一切都正常了,感谢你超快的响应。
发布于 2019-03-13 08:59:56
我使用了这段代码,它对我来说很有效
//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);https://stackoverflow.com/questions/7462997
复制相似问题