What process locks the file?

  • Thread starter Thread starter eugenet
  • Start date Start date
E

eugenet

Hello,

This is probably a newbie question.

In my C# program, when I try 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 explain it in Windows 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
 
In my C# program, when I try 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.

Whoa, terminating random processes isn't something a well behaving
application should do. There are legitimate reasons why a file can be
locked. Consider for example if there's an antivirus application
running and it happens to lock the file during scanning at the same
time you try to delete it. You don't want to kill the antivirus tool
just because of that, do you? Can't you just pop up a message to the
user that says the file is locked and ask her to close whatever app is
holding it and then try again?


Mattias
 
This file I need to delete is an Excel workbook. I use WebBrowser
control to open an Excel spreadsheet. WebBrowser control spawns an
Excel process. The problem is that once the form that hosts the
WebBrowser control is closed, this Excel process tends to stay in
memory for at least sometime (sometimes until manually terminted);
therefore the problem -- I cannot clean up after my application, and
there is no visible Excel on the desktop -- just a process in the Task
Manager.

Is there anything obvious I am missing? Is that possible to grab the
process ID of the spawned Excel process through the WebBrowser control?

Thanks.

Evgueni
 
Is there anything obvious I am missing? Is that possible to grab the
process ID of the spawned Excel process through the WebBrowser control?

Not that I'm aware of. But I would assume there's some way to get to
the Excel automation object model so you can call Quit on it to make
it terminate properly. Unfortunately I don't know enough about the web
browser to say exactly how.


Mattias
 
Back
Top