Click here to Skip to main content
15,891,902 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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...



EDIT: bring code from comment below

C#
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
   }
Posted
Updated 29-Apr-22 21:29pm
v4

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.
 
Share this answer
 
Comments
beh7606 25-Mar-12 5:29am    
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
}
this is my code sample...got any idea?
OriginalGriff 25-Mar-12 5:58am    
File.ReadAllLines should be ok - the documentation doesn't refer to any locks, but internally it uses a stream which it closes, so it should be ok.
It might be worth trying using the stream yourself to see if it cures the problem:

using (FileStream fs = new FileStream(@"D:\Temp\myLargeTextFile.txt", FileMode.Open, FileAccess.Read))
{
using (StreamReader sr= new StreamReader(fs))
{
string all = sr.ReadToEnd();
string[] lines = all.Split('\n');
...
}
}

Otherwise you have to look at what else is going on in your system - both in terms of the rest of you app, but also the environment in which it runs.
There is code here: http://stackoverflow.com/questions/860656/how-does-one-figure-out-what-process-locked-a-file-using-c
and here: http://stackoverflow.com/questions/317071/how-do-i-find-out-which-process-is-locking-a-file-using-net
which will tell you the lock source process.
Shahin Khorshidnia 25-Mar-12 9:29am    
My +5
yes, There is no way if it's using by another application except re-tring.
filled with eror 32;the procse cannot access use another proces
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900