Windows Develop Bookmark and Share   
 index > Windows Forms General > What process locks the file?
 

What process locks the file?

Hello,

This is probably a newbie question.

In my C# program, whenItry to delete a file, I get an exception that the file is locked by another process. I would like to terminate the process that locks the file.

How can I determine what process has a lock on a file, just based on the file path?If you have an example in .NET, that would be super, but if you could explainit inWindows API terms, that will do as well.

Thanks for your help!

Evgueni

PS. I have to be able to solve this programmatically -- sysinternals' tools cannot be used

Evgueni  Tuesday, August 29, 2006 2:48 PM
you can't really do this im afraid, just by the file path - there isn't a way of finding out what application has the access to that file, and even if you do find it - terminating it would be a bad thing as it can cause undesirable side effects. There maybe a way but I certainly wouldn't recommend terminating it prematurely
ahmedilyas  Tuesday, August 29, 2006 3:14 PM

Take a look at this :

http://blogs.msdn.com/calvin_hsia/archive/2005/06/02/424586.aspx

This may or may not help you, but provides some insight into the problem, atleast....

best

sriram

SRIRAM RAJAMANURI  Tuesday, August 29, 2006 3:50 PM

Thanks, but that really doesn't help since I need a way to find the process ID of the process that puts a lock on a file, and the only known parameter is the file name.

It must be somehow possible. After all, SysInternals guys are able to do that somehow ...

Thanks.

Evgueni

Evgueni  Tuesday, August 29, 2006 6:40 PM
They've spent untold hours reverse-engineering the undocumented format of the kernel object tables. Give them a call, maybe they'll share this insight with you. Hehe...
nobugz  Wednesday, August 30, 2006 3:34 AM
I've used the following method with good results to unlock a file with a process handle to it:

/// <summary>
/// Unlocks the file.
/// </summary>
/// <param name="fileName">Name of the file.</param>
private static void UnLockFile(string fileName)
{
FileStream fs = new FileStream(fileName, FileMode.Open);
try
{
fs.Unlock(0, fs.Length);
}
catch
{
// do nothing
}
finally
{
fs.Close();
fs.Dispose();
}
}
MMarais  Friday, May 30, 2008 2:09 AM

You can use google to search for other answers

Custom Search

More Threads

• are windows forms a replacement for mfc?
• Update a rectangle
• Arranging the controls in the toolstrip one on top of each other
• ToolStrip Color Picker
• currency label read from one global variable
• where should i put businesslogiclayer
• Manipulating the applications taskbar button.
• multilingual software in vb.net.
• Alternative for FindWindowEx?
• OpenFileDialog