如何解决无法访问另一个进程正在使用的文件 [英] how to solve cannot access the file it is being used by another process

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

问题描述

我创建了一个使用文件监视程序的应用程序..
此应用程序在
时读取csv文件(文本文件) 该文件上有更改并将其存储在Db中...

一切都很好...好几次了,直到
我的应用程序产生了一个错误..

i create an application which using file watcher..
this application read csv file(text file) when
there is changes on that file and store it in Db...

everything working just fine...for several time until
my application produce an error..
saying

cannot access the file because it being used by another process...

请帮助...



从下面的注释中获取代码

help please...



bring code from comment below

string[] content = System.IO.File.ReadAllLines(e.FullPath); //

   foreach (string line in content)
   {
      string[] arr;
      arr = line.Split(','); //splitting the line which was read
      // create data table
   }

推荐答案

我们无法使用特定代码来回答-可能是一件大事!但是您可以执行一些常规操作:
1)检查您用于读取文件的代码-如果您使用任何流或其他与文件相关的对象,则应将其尽快处置.将它们包含在using块中是一个好主意,因为这样可以确保它们在处理后超出范围,并且以后不能使用.
2)尝试读取和存储更新的文件时,可能是另一个应用程序正在使用该文件-在这种情况下,您需要稍后重试.我确实打开文件,更改文件,退出然后再返回,因为我忘记了一些东西.您的用户可能正在这样做.如果是这种情况,那么您需要在程序中添加异常处理,以便您可以优雅地响应,而不会因异常而崩溃.不过请先检查(1)-您拥有的所有流都不会被GC释放,因此,适当的处理只会使您的程序以更大的方式失败.
We can''t answer this with an specific code - it could be anthing! But there are general things you can do:
1) Check you code for reading the file - if you use any streams or other File related objects, they should be disposed as soon as possible. Enclosing them in a using block is a good idea, as it insures they go out of scope when they are disposed and cannot be used afterwards.
2) It may be that the updated file is in use by another application when you try to read it and store it - in which case you need to re-try later. I do open files, change them, exit and then go back in because I forgot something. It may be that your users are doing the same. If this is the case, then you need to add exception handling to you program so that you can respond gracefully instead of crashing with an exception. Do check (1) first though - any streams you hold will not be released until the GC gets round to it, so graceful handling will just make your program fail in bigger ways.


这篇关于如何解决无法访问另一个进程正在使用的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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