views:

56

answers:

2

I'm developing a multi-threaded ASP.NET 3.5 application, during working with some file, I'm getting the following exception :

The process cannot access the file because it is being used by another process

I'm looking for a way to know exactly which process is locking that file so I can stop its access to the file. OR if that is complicated, I want a way to lock the file by my thread so other threads/processes can't access it. Thanks.

+1  A: 

Process Explorer will allow you to do this. Simply click "Find" -> "Find Handle or DLL", then enter the name of your file.

It will then show you every process with a handle to that file.

RB
+1 for Proc Explorer - a very useful too.
Steve Haigh
Already tried that, but that unknown process used the file for a very short period of time, so I had no luck with catching it!
Moayad Mardini
Hmm - in that case, I'd probably start adding logging of file opening to my application, to try and trace down which thread is opening it. I'm sure there will be someone along in a sec to tell you a great way using mutexes or something though.
RB
Sounds like a virus scanner. Have you tried disabling it if you have one?
RichardOD
Yes, I did, still no luck.
Moayad Mardini
A: 

To actually lock access to the resource (the 2nd option you suggest), you could use a Mutex (a Mutual Exclusion).

I've not done any of this in .NET, but there seems to be good articles on it.

RB