ahmd0
ahmd0

Reputation: 17293

How to know what keeps a lock on a file

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?

Upvotes: 1

Views: 101

Answers (1)

Alexei Levenkov
Alexei Levenkov

Reputation: 100547

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.

Upvotes: 1

Related Questions