我有以下复制文件的代码:
var copedFile = ConfigurationManager.AppSettings["PathToFirebirdDB"] + ".001";
using (var inputFile = new FileStream( ConfigurationManager.AppSettings["PathToFirebirdDB"],
FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (var outputFile = new FileStream(copedFile, FileMode.Create))
{
var buffer = new byte[0x10000];
int bytes;
while ((bytes = inputFile.Read(buffer, 0, buffer.Length)) > 0)
{
outputFile.Write(buffer, 0, bytes);
}
}
}这段代码只工作一次。下次我收到下面的消息时:
The process cannot access the file 'D:\Programs\IBExpert\db.fdb.001' because it is being used by another process. System.IO.IOException: The process cannot access the file 'D:\Programs\IBExpert\db.fdb.001' because it is being used by another process.为什么?有using块。
发布于 2012-09-27 18:01:17
如果您在关闭文件后尝试重新打开该文件,则系统仍有可能认为该文件处于打开状态,因为它实际上是打开的。
一个典型的原因是病毒扫描程序保持文件打开以确保它没有被感染,这在后台发生,并可能在您自己关闭文件后继续运行。
发布于 2012-09-27 17:52:18
可能是因为您没有关闭这些文件。
顺便说一句,为什么不直接使用File.Copy呢?
https://stackoverflow.com/questions/12618559
复制相似问题