【发布时间】:2016-08-29 08:35:06
【问题描述】:
我正在尝试将所有 PDF 文件 (.pdf) 从源文件夹复制到目标。我已经使用foreach 循环编写了它,但我想在没有 for 或任何循环的情况下执行它。有没有办法做到这一点,如果有,怎么做?
我的代码
string sourcePath = @"D:\DataArchiveTest\From";
string targetPath = @"D:\DataArchiveTest\To";
foreach (var sourceFilePath in Directory.GetFiles(sourcePath))
{
string fileName = Path.GetFileName(sourceFilePath);
string destinationFilePath = Path.Combine(targetPath, fileName);
if (fileName.ToUpper().Contains(".PDF"))
{
System.IO.File.Copy(sourceFilePath, destinationFilePath, true);
}
}
【问题讨论】:
-
实际上sourceFolder 可能包含数千个文件。如果我们使用 for 循环,那么它会影响我的应用程序的性能
标签: c# file pdf directory copy