1

While trying to read a file from my ASP.NET web application using this method:

string strContents;
using (StreamReader sr = new StreamReader(strFilePath))
{
    strContents = sr.ReadToEnd();
}

I get the following exception:

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

So I'm curious, is there any way to know what's locking this file?

PS. It'd be nice to know this from inside the exception and if that's not possible, is there any way to know it somehow else?

whastupduck
  • 1,156
  • 11
  • 25
ahmd0
  • 16,633
  • 33
  • 137
  • 233
  • 2
    You have't heard of [Process Explorer](http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx)? It is pretty standard, I thought everyone knew about it. – Jeremy Thompson Mar 15 '13 at 05:09
  • I heard about it. I thought more about knowing it from my process first. – ahmd0 Mar 15 '13 at 05:10
  • 1
    In the end you will probably realize its the `w3wp` process. Killing a process is not healthy but [Unlocker](http://www.emptyloop.com/unlocker/) is your friend. – Jeremy Thompson Mar 15 '13 at 05:13
  • @JeremyThompson: `Unlocker` seems like a nifty little tool. Thanks. – ahmd0 Mar 15 '13 at 05:30
  • @JeremyThompson: Wow, you got it right. It is `w3wp`. I got it from the `Handles` routine. What the heck is it? And why is it holding a lock on my file? Is there any way to prevent it from doing it? – ahmd0 Mar 15 '13 at 07:07

1 Answers1

1

Standard answer is to use one of the SysInternals tools like handle to see what process locks the file.

If you are sure it is your code - code review may be easier than digging through dump of the process with WinDbg.

If you want to write your own - reading "Windows Internals" book is essentially a must and good knowledge of interop would be plus.

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • OK, I see. So this info is not included in the exception when it's thrown, is it? – ahmd0 Mar 15 '13 at 05:13
  • @ahmd0 - No. To my knowledge the only information that could be available is process ID, but to get even that in many cases process would need higher privileges to find it out. So there is no information in the exception (and as far as I know there is no built in way of getting it). – Alexei Levenkov Mar 15 '13 at 05:18
  • @ahmd0, if it is your process Code analysis (FxCop) in Visual Studio (if you have it your edition) may help you to find cases where you don't properly dispose Reader/Stream object (see http://stackoverflow.com/questions/2214605/is-there-a-fxcop-rule-for-local-used-idisposables) – Alexei Levenkov Mar 15 '13 at 05:20
  • As I just found out using the `handle` tool you suggested that the process that holds the lock on my file is `w3wp`. I posted an update above. Is there any way to prevent it from locking, or even opening my file? – ahmd0 Mar 15 '13 at 07:13
  • @ahmd0 :) w3wp is your process - w3wp is the process hosting ASP.Net run-time for IIS (or any other run-time serving pages - hence the name "World Wide Web Worker Process"). So your code somewhere forgets to release the lock (likely missing some using around file usage). – Alexei Levenkov Mar 15 '13 at 18:00
  • Thanks. Indeed that was the case. – ahmd0 Mar 16 '13 at 08:18