【发布时间】:2012-04-12 09:13:08
【问题描述】:
我正在尝试创建一个目录并在 Parallel.ForEach 中复制一个文件 (pdf)。
下面是一个简单的例子:
private static void CreateFolderAndCopyFile(int index)
{
const string sourcePdfPath = "c:\\testdata\\test.pdf";
const string rootPath = "c:\\testdata";
string folderDirName = string.Format("Data{0}", string.Format("{0:00000000}", index));
string folderDirPath = rootPath + @"\" + folderDirName;
Directory.CreateDirectory(folderDirPath);
string desPdfPath = folderDirPath + @"\" + "test.pdf";
File.Copy(sourcePdfPath, desPdfPath, true);
}
上述方法创建一个新文件夹并将pdf文件复制到一个新文件夹中。 它创建了这个目录树:
TESTDATA
-Data00000000
-test.pdf
-Data00000001
-test.pdf
....
-Data0000000N
-test.pdf
我尝试在Parallel.ForEach 循环中调用CreateFolderAndCopyFile 方法。
private static void Func<T>(IEnumerable<T> docs)
{
int index = 0;
Parallel.ForEach(docs, doc =>
{
CreateFolderAndCopyFile(index);
index++;
});
}
当我运行此代码时,它会出现以下错误:
进程无法访问文件'c:\testdata\Data00001102\test.pdf' 因为它正被另一个进程使用。
但在我收到此错误之前,它首先创建了 1111 个新文件夹并复制了 test.pdf 大约 1111 次。
是什么导致了这种行为,如何解决?
已编辑:
上面的代码是玩具示例,对于硬编码字符串感到抱歉 结论:并行方法很慢。
明天我试试How to write super-fast file-streaming code in C#?的一些方法。
尤其是:http://designingefficientsoftware.wordpress.com/2011/03/03/efficient-file-io-from-csharp/
【问题讨论】:
-
顺便说一句,您可能应该使用
Path.Combine而不是自己连接路径。 -
@Mike 我注意到您尚未在网站上投票或接受答案。我建议您阅读 faq 以了解 Stack Overflow 社区的这些方面。
标签: c# parallel-processing file-copying