因为它正由另一个进程的进程无法访问该文件 [英] The process cannot access the file because it is being used by another process

查看:137
本文介绍了因为它正由另一个进程的进程无法访问该文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到从SQL Server数据库字段的二进制数据和我在一个目录我的应用程序有权限在本地创建一个文档。不过我仍然得到的称号指定的错误。我曾尝试在网上公布包括那些在#2 previous帖子上提出许多建议。我也用ProcessExplorer>查找处理定位锁,它没有返回值,如果该文件没有锁定。

我使用的是code下面将文件保存到文件系统,然后我尝试将该文件在应用过程中的另一种方法中复制到新的位置。正是这种复制方法,它采用新创建的文件抛出异常的路径。

文件本身与内容创建,我可以通过Windows资源管理器中打开它,没有任何问题。

我失去的东西完全明显?我是从数据库中正确地创建该文件?解决或更好的诊断问题的任何帮助将非常AP preciated。

  //从数据库获取文件
的FileStream FS =新的FileStream(
    C:\\ myTempDirectory \\ myFile.doc,FileMode.OpenOrCreate,FileAccess.Write);
BR的BinaryWriter =新的BinaryWriter(FS);
br.Write(BinaryDataFromDB);
fs.Flush();
fs.Close();
fs.Dispose();//拷贝文件
File.Copy(sourceFileName,目的地文件名,真实);


解决方案

尝试加入 GC.Collect的()的调用您已经处理你的数据流给力的后垃圾收集器来清理。

  //从数据库获取文件
使用(的FileStream FS =新的FileStream(C:\\ myTempDirectory \\ myFile.doc,FileMode.OpenOrCreate,FileAccess.Write))
使用(BR的BinaryWriter =新的BinaryWriter(FS))
{
   br.Write(BinaryDataFromDB);
   fs.Flush();
}//强制清理
所以GC.Collect();//拷贝文件
File.Copy(sourceFileName,目的地文件名,真实);

I am getting binary data from a SQL Server database field and am creating a document locally in a directory my application has permissions. However I am still getting the error specified in the title. I have tried numerous suggestions posted on the web including those suggested in previous posts on Stackoverflow. I have also used ProcessExplorer > Find Handle to locate the lock and it returns nothing as if the file is not locked.

I am using the code below to save the file to the file system and I then try to copy this file to a new location later in the application process within another method. It is this copy method that takes the path of the newly created file that throws the exception.

The file itself is created with its content and i can open it through Windows Explorer without any problems.

Am I missing something completely obvious? Am I creating the file correctly from the database? Any help on solving or better diagnosing the problem would be much appreciated.

// Get file from DB
FileStream fs = new FileStream(
    "C:\myTempDirectory\myFile.doc", FileMode.OpenOrCreate, FileAccess.Write);
BinaryWriter br = new BinaryWriter(fs);
br.Write("BinaryDataFromDB");
fs.Flush();
fs.Close();
fs.Dispose();

// Copy file
File.Copy(sourceFileName, destinationFilename, true);

解决方案

Try adding a call to GC.Collect() after you have disposed of your streams to force the garbage collector to clean up.

// Get file from DB 
using(FileStream fs = new FileStream("C:\myTempDirectory\myFile.doc", FileMode.OpenOrCreate, FileAccess.Write)) 
using(BinaryWriter br = new BinaryWriter(fs))
{
   br.Write("BinaryDataFromDB"); 
   fs.Flush(); 
}

//Force clean up
GC.Collect();

// Copy file 
File.Copy(sourceFileName, destinationFilename, true); 

这篇关于因为它正由另一个进程的进程无法访问该文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆