Why is process locking directroy?

  • Thread starter Thread starter J. Clay
  • Start date Start date
J

J. Clay

I have developed a Windows Service application that uses a file watcher to
watch for new files being dumped into an directroy. As soon as it sees a
new file, it moves it to a different directory for it to be processed. In
the processing I use a streamreader to pull each line for parsing into a
database.

The problem, is that the first file after the service is started works as
expected, but any subsequent files that is put in the directory cannot be
moved to the processing directory. I get the following error message:

"The process cannot access the file "\\Server\Share\Directory\newfile.txt"
because it is being used by another process."

After using File.Move(OrigFile, NewFile) I call the processing class which
uses a streamreader that I close and set to nothing after completion. I
then set the processing class to nothing.

Any suggestions or ideas??

TIA,
Jim
 
Hi Jim,

How did you dump the file into the dir?
Can you show a small reproduce sample?
Also you may try to use the tool below to see which process locked file and
then check that process.
Handle
http://www.sysinternals.com/utilities/handle.html

e.g.
handle test.txt

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
I did some more testing and discovered that if I wait a while an additional
file will work fine. At this point it is good enough as this is going to be
a daily upload to an FTP folder. But for some reason, it appears that the
Stream Reader is locking the directory for a period of time...

Jim
 
Hi

I guess that may be the cache mechanism.
You may try to call Close on the StreamReader and then set it to Nothing,
and then call the below to force the GC to collect.
GC.Collect()
GC.WaitForPendingFinalizers()

Also you may try to use the File class directory to see if that have any
help.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemiofilememberstopic.asp


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top