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
 
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.
 
Back
Top