Posted By: Zeus | Mar 3rd, 2008 @ 4:01 AM
page 1 of 1
Comments: 2 | Views: 2892
Zeus
Zeus
Why is the caption missing??
Original file here

Hi guys.

I am trying to develop a simle windows service (my first windows service) that watches a folder on a server. It uses the System.IO.FileSystemWatcher class to watch a folder, with only the created event raised and the FileName NotifyFilter.

The thing is, it works for the first 1-3 files I drop into the folder, imports into the DB without issues.

But when I drop the 4th file and onwards, I get a file is locked error.

I am always dropping in new files, not files that are already in the folder.

Anyone have an idea as to why the files are beging locked ?
TommyCarlier
TommyCarlier
I want my scalps!
I don't know why you get the error, but I have some remarks. First of all, the lock is useless because you're just creating a new object each time.
Secondly, concatenating the lines like that is far from optimal: it creates a lot of string objects that are unnecessary. There are actually functions in the .NET Framework to read an entire file. You can replace your entire StreamReader-block with this line:
string fileContent = File.ReadAllText(e.FullPath, Encoding.Default);

I don't know why you get the "file is locked" error, but I don't think it's your code that locks the file. It's the application that drops the file in the folder. If you want to reliably be able to read the file, you'll have to try to read it multiple times. A while ago, I've written a class that does this automatically: a wrapper around FileSystemWatcher. You can find it here: http://tommycarlier.blogspot.com/2007/01/filecreationwatcher.html
page 1 of 1
Comments: 2 | Views: 2892
Microsoft Communities