FileStream.close()不会释放文件的其他进程 [英] FileStream.close() does not free file for other processes

查看:781
本文介绍了FileStream.close()不会释放文件的其他进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的Page_Load中调用的函数code。当页面正在启动Visual Studio中加载后的第一次,一切顺利的罚款。结果
但是,任何其他开放调用后的文件返回 IOException异常:文件正在被另一个进程使用,即使直接打开该文件中的VisualStudio解决方案将返回此错误(当然不是例外)

 的FileStream mailinglist_FileStream =新的FileStream(@\\ foobarFile.txt,FileMode.Open);
PeekingStreamReader mailinglist_Reader =新PeekingStreamReader(mailinglist_FileStream);
//做一些东西与文件
mailinglist_FileStream.Close();
mailinglist_Reader.Close();
mailinglist_Reader.Dispose();
mailinglist_FileStream.Dispose();

为什么文件仍然锁定?为什么不完全重新启动Visual Studio中重置文件?
检查文件的属性时,它说:


  

生成操作:搜索内容
  复制到输出目录:不复制


我只是阅读本文件。我可以做类似的东西,以 ADLOCKOPTIMISTIC ,以便多个进程可以访问文件?


解决方案

  

为什么文件仍然锁定?为什么不完全重新启动视觉
  工作室重置文件?当检查文件属性它说[...]
  我不知道为什么该文件仍然锁定:可能是因为你的code失败之前流被关闭/处置


关于为什么完全重新启动Visual Studio的[...]的:因为​​您可能正在使用IIS防爆preSS或ASP.NET开发服务器的当您关闭IDE关闭,等文件锁,因为进程持有的锁不再运行释放出来。

和有关的为什么是文件仍然锁定?[...] 的这可能是因为文件流没有关闭,因为有时线程可能无法成功地结束,锁AREN ŧ释放。

由于对方回答说,检查有使用块可避免的IDisposable 对象不会被处理:

  // FileShare.ReadWrite将允许其他进程
//读写目标文件,即使其它进程
//都具有相同的文件工作
使用(的FileStream mailinglist_FileStream =新的FileStream(@\\ foobarFile.txt,FileMode.Open,FileShare.ReadWrite))
使用(PeekingStreamReader mailinglist_Reader =新PeekingStreamReader(mailinglist_FileStream))
{
      //做你的东西。使用块将调用Dispose()为
      //你,即使出了问题,因为它是等于一个try /终于来了!
      //同时检查了如何使用语句可以无需额外被链接{}
 }


  

我只是阅读本文件。我可以做类似的东西来
  ADLOCKOPTIMISTIC,使多个进程可以访问该文件?


是的,看看 File.Open 方法和文件共享枚举:

I have Following Code in a Page_Load called function. When the Page is loaded the first time after starting Visual Studio, everything works out fine.
But any other opening call to the File after that returns IOException: "File is in use by another process", even when directly opening the File in VisualStudio Solution this Error is returned(of course not as Exception)

FileStream mailinglist_FileStream = new FileStream(@"\foobarFile.txt", FileMode.Open);
PeekingStreamReader mailinglist_Reader = new PeekingStreamReader(mailinglist_FileStream);
//Do some stuff with the file
mailinglist_FileStream.Close();
mailinglist_Reader.Close();
mailinglist_Reader.Dispose();
mailinglist_FileStream.Dispose();

Why is the file still locked? and why does fully restarting Visual Studio reset the File? when checking file-Properties it says:

Build Action: Content
Copy to output directory: do not Copy

I am only reading this File. can i do something similiar to adLockOptimistic, so that multiple processes can access the File?

解决方案

Why is the file still locked? and why does fully restarting Visual Studio reset the File? when checking file-Properties it says [...] I don't know why the file is still locked: probably because your code fails before the stream is closed/disposed.

About "why fully restarting Visual Studio [...]": because you may be using IIS Express or ASP.NET Dev Server whose are closed when you close the IDE, so locks on files are released since the process holding the locks is no longer running.

And about "why is the file still locked?[...]" it could be because the file stream isn't closed because sometimes the thread may not end successfully and the locks aren't released.

As other answer said, check how using block may avoid that IDisposable objects wouldn't be disposed:

// FileShare.ReadWrite will allow other processes 
// to read and write the target file even if other processes 
// are working with the same file
using (FileStream mailinglist_FileStream = new FileStream(@"\foobarFile.txt", FileMode.Open, FileShare.ReadWrite))
using (PeekingStreamReader mailinglist_Reader = new PeekingStreamReader(mailinglist_FileStream))
{
      // Do your stuff. Using blocks will call Dispose() for 
      // you even if something goes wrong, as it's equal to a try/finally! 
      // Also check how using statements can be chained without extra { }           
 }

I am only reading this File. can i do something similiar to adLockOptimistic, so that multiple processes can access the File?

Yes, take a look at File.Open method and FileShare enumeration:

这篇关于FileStream.close()不会释放文件的其他进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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