.net 将文件复制到其他目录

jm81lzqq  于 12个月前  发布在  .NET
关注(0)|答案(8)|浏览(185)

我在一个项目中工作,我想复制一个目录中的一些文件到第二个已经存在的目录。
我找不到一种方法来简单地从一个文件夹复制到另一个。我可以找到复制文件到一个新的文件,或目录到一个新的目录。
我现在设置程序的方法是,复制文件,并将其保留在同一个目录中,然后将该副本移动到我想要的目录中。
编辑:
谢谢大家。你所有的答案都奏效了。我意识到我做错了什么,当我设置目标路径时,我没有添加文件名。一切工作现在,感谢您的超级快速React.

myzjeezk

myzjeezk1#

这对我很有效:

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

r1zhe5dt2#

如果目标目录不存在,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 subdirectories, 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上测试

pgx2nnw8

pgx2nnw83#

我用这个代码和它为我工作

//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);
lpwwtiir

lpwwtiir4#

NET 6-扩展方法

[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);
b0zn9rqh

b0zn9rqh5#

string fileToCopy = "c:\\myFolder\\myFile.txt";
string destinationDirectory = "c:\\myDestinationFolder\\";

File.Copy(fileToCopy, destinationDirectory + Path.GetFileName(fileToCopy));
xesrikrc

xesrikrc6#

File.Copy(@"someDirectory\someFile.txt", @"otherDirectory\someFile.txt");

工作正常.

s4n0splo

s4n0splo7#

MSDN File.Copy

var fileName = "sourceFile.txt";
var source = Path.Combine(Environment.CurrentDirectory, fileName);
var destination = Path.Combine(destinationFolder, fileName);

File.Copy(source, destination);
mrwjdhj3

mrwjdhj38#

也许

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

相关问题