[英]copying file, file not disposed
我有以下代碼用於復制文件:
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
塊。
如果您嘗試在關閉文件后立即重新打開該文件,則系統實際上仍會將該文件視為打開狀態。
一個典型的原因是病毒掃描程序一直在打開文件以確保它沒有被感染,這是在后台發生的,在您自己關閉文件后可能會繼續運行。
可能是因為您沒有關閉文件。
順便說一句,為什么你不只是使用File.Copy
?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.