Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

One thing that annoys me no end about Windows is the old "sharing violation" error. Often you can't identify what's holding it open. Usually it's just an editor or explorer just pointing to a relevant directory but sometimes I've had to resort to rebooting my machine.

Any suggestions on how to find the culprit?

share|improve this question
    
Awesome, I've had this problem on soo many occassions. –  tsilb May 1 '09 at 3:15
3  
You would think that after all this time, the Windows guys would give us a way to do this easily from within Explorer. I wonder why this hasn't happened? –  Andrew H May 5 '09 at 0:19
    
I find that Explorer is very often the problem process that is holding onto a file for no obvious reason. –  Eddie May 8 '09 at 15:49
    
I know this doesn't help you much, but I think I remembered that this was a planned feature of the next Windows release after vista and 2008 server. or maybe it's a WinFS thing. not sure where i read that... –  Kip May 30 '09 at 0:00

15 Answers 15

I've had success with Sysinternals Process Explorer. With this, you can search to find what process(es) have a file open, and you can use it to close the handle(s) if you want. Of course, it is safer to close the whole process. Exercise caution and judgement.

To find a specific file, use the menu option "Find->Find Handle or DLL..." Type in part of the path to the file. The list of processes will appear below.

If you prefer command line, Sysinternals suite includes Handle, that lists open handles. A few examples on how to use it:

  • c:\Program Files\SysinternalsSuite>handle.exe |findstr /i e:\ - find all files opened from drive E:
  • c:\Program Files\SysinternalsSuite>handle.exe |findstr /i file-or-pathi-in-question
share|improve this answer
10  
You can close the handle, but keep in mind, you're pulling the rug out from under an application, results will be unpredictable at best. –  WaldenL May 1 '09 at 21:57
11  
@Walden: Absolutely. YMMV. With WinXP, I've many times had Explorer open a handle for no obvious reason and refuse to close it. When this happens on a file you need to delete, you have the choice of forcing the handle closed, or rebooting. So far, having done this dozens of times, I have suffered no ill effect. As with any advanced tool, use with caution and judgment. –  Eddie May 1 '09 at 22:32
2  
Closing the handles can cause the app to re-use the handle on another file, causing corruption - see Jeff's answer below: serverfault.com/a/15695/79266 ... much safer to kill the application holding the file open, if you don't want to reboot. –  RichVel Mar 30 '12 at 8:32
7  
For explorer, btw, hold ctrl-shift and right-click a blank area of the start menu, and you'll get "Exit Explorer" - ps, not quite Jeff's answer.. –  Mark Sowul Apr 2 '12 at 18:56
    
@RichVel : thanks for pointing that out! –  David Aug 10 '12 at 19:22

unlocker is also useful for this (works on both 32 and 64 bit)

share|improve this answer
    
Have used unlocker for quiet sometime. It is very useful. –  Phantom Yoda May 1 '09 at 0:58
5  
If only it worked in 64-bit, it'd be perfect. Had to move over to Process Explorer when I upgraded my OS. –  bdukes May 29 '09 at 16:48
2  
Note that unlike Procexp, Unlocker will only detect open handles in the immediate context. If you run Unlocker on a parent directory and have a file open a few subdirectories down, Unlocker won't tell you about it. –  Unsigned Oct 17 '11 at 17:59
2  
It does support x64 now, as of 1.9.0 –  Mordachai Feb 13 '12 at 17:15
5  
Be very careful - the current installer will install the notorious Babylon toolbar by default. –  Mike Chamberlain Feb 12 '13 at 10:18

Try the openfiles command.

share|improve this answer
5  
+1 for a builtin command, although I personally use ProcessExplorer for this most of the time. –  RBerteig May 6 '09 at 2:04
    
Really helpful. –  marko Sep 22 '11 at 8:40
2  
ERROR: The target system must be running a 32 bit OS. –  Bozojoe Oct 24 '12 at 17:29

Just be very careful with closing handles; it's even more dangerous than you'd think, because of handle recycling - if you close the file handle, and the program opens something else, that original file handle you closed may be reused for that "something else." And now guess what happens if the program continues, thinking it is working on the file (whose handle you closed), when in fact that file handle is now pointing to something else.

see Raymond Chen's post on this topic

Suppose a search index service has a file open for indexing but has gotten stuck temporarily and you want to delete the file, so you (unwisely) force the handle closed. The search index service opens its log file in order to record some information, and the handle to the deleted file is recycled as the handle to the log file. The stuck operation finally completes, and the search index service finally gets around to closing that handle it had open, but it ends up unwittingly closing the log file handle.

The search index service opens another file, say a configuration file for writing so it can update some persistent state. The handle for the log file gets recycled as the handle for the configuration file. The search index service wants to log some information, so it writes to its log file. Unfortunately, the log file handle was closed and the handle reused for its configuration file. The logged information goes into the configuration file, corrupting it.

Meanwhile, another handle you forced closed was reused as a mutex handle, which is used to help prevent data from being corrupted. When the original file handle is closed, the mutex handle is closed and the protections against data corruption are lost. The longer the service runs, the more corrupted its indexes become. Eventually, somebody notices the index is returning incorrect results. And when you try to restart the service, it fails because its configuration files have been corrupted.

You report the problem to the company that makes the search index service and they determine that the index has been corrupted, the log file has mysteriously stopped logging, and the configuration file was overwritten with garbage. Some poor technician is assigned the hopeless task of figuring out why the service corrupts its indexes and configuration files, unaware that the source of the corruption is that you forced a handle closed.

share|improve this answer
1  
+1 for the link. –  Unsigned Oct 17 '11 at 18:03
5  
important warning, this should go nearer the top - a reboot is probably better than a silently corrupted file. –  RichVel Mar 30 '12 at 8:27
    
Wow, very good point Mark! –  David Aug 10 '12 at 19:19

I've used Handle with success to find such processes in the past.

share|improve this answer
3  
I use this tool all the time. If I can't eject a USB drive, I just type "handle H:" (or whatever the drive letter). Best of all, you can use it to force close handles. –  Chris Thompson May 2 '09 at 7:02

For Windows 7 and Windows 8 you can use the built-in Resource Monitor for this.

  1. Open Resource Monitor, which can be found
    • By searching in the start menu, or
    • As a button on the Performance tab in your Task Manager
  2. Use the search field in the Associated Handles section on the CPU tab
    • Pointed at by blue arrow in screen shot below

In case it's not obvious, when you've found the handle, you can identify the process by looking at the Image and/or PID column.

You can then close the application if you are able to do that, or just right-click the row and you'll get the option of killing the process right there. Easy peasy!

Resource Monitor screenshot

Copied from my original answer: http://superuser.com/a/643312/62

share|improve this answer

Who Lock Me works well and keeps people amused with the name!

share|improve this answer

Apropos Explorer holding a file open: "When this happens on a file you need to delete, you have the choice of forcing the handle closed, or rebooting."

You can just end Explorer.

If this is a one-time thing (Explorer does not normally hold this file open) then I would guess logging off and logging back on will do the trick.

Otherwise, kill the desktop Explorer process and do what you want while it's gone. First start a copy of cmd.exe (you need a UI to do your intended cleanup). Make sure there are no non-desktop Explorers running. Then kill the last Explorer with, e.g., Task Manager. Do what you want in the command prompt. Finally, run Explorer from the command prompt, and it will become the desktop.

I'd guess there may be some residual unpleasantness if some systray programs can't deal with the shell restarting.

share|improve this answer
    
This is much safer than closing the handle, and less disruptive than rebooting. Applies to other programs too - I often find a Microsoft Office program holds locks on files even after I've closed them. –  RichVel Mar 30 '12 at 8:30

Just to clarify, this is more likely to be a result of misbehaving 3rd party apps not using the CreateFile API call correctly than it is to be anything in Windows itself. Perhaps it's a consequence of the design of CreateFile, but done is done and we can't go back.

Basically when opening a file in a Windows program you have the option to specify a flag that allows shared access. If you don't specify the flag, the program takes exclusive access of the file.

Now, if Explorer seems to be the culprit here, it may be the case that that's just on the surface, and that the true culprit is something that installs a shell extension that opens all files in a folder for it's own purposes but is either too gung-ho in doing so, or that doesn't clean up properly after itself. Symantec AV is something I've seen doing this before, and I wouldn't be surprised if other AV programs were also to blame. Source control plug-ins may also be at fault.

So not really an answer, but just some advice to not always blame Windows for what may be a badly written 3rd party program (something that can also happen on any other OS which has implicit file locking, but any unix based OS has shared access by default).

share|improve this answer
    
I won't blame a 3rd party app on the fact that my windows explorer always locks Thumbs.db files in its working directory, but does not always unlock it when I switch working directory. At least they are unlocked when I close the window, I don't have to kill ALL explorers... –  Alexander Mar 7 at 7:32

On a remote server, when you're checking on a network share, something as simple as the Computer Management console can display this information and close the file.

share|improve this answer

Files can be locked by local processes (unlocker is the tool to use) and by file access that comes in through shares.

There is a built-in function in Windows that shows you what files on the local computer are open/locked by remote computer (which has the file open through a file share):

* Select "Manage Computer" (Open "Computer Management")
* click "Shared Folders"
* choose "Open Files"

There you can even close the file forcefully.

share|improve this answer

There is NirSoft's Opened Files View as well.

share|improve this answer
    
Personally I like this better than Process Explorer for this specific tasks. –  JamesBarnett Feb 25 '13 at 19:38

Lockhunter (http://lockhunter.com/) works on 32 and 64bit systems.

share|improve this answer

I got turned on to the Exteneded Task Manager a while ago by Jeremy Zawodny's blog, and it's great for tracking down further info on processes too. +1 for Process Explorer as above, too, especially for killing processes that the standard Task Manager won't end.

share|improve this answer

There is a tool FILEMON and shows open files and handles. Its hard to keep up with its display if you watch it live, it does so quickly. But you can stop it from displaying live and you can watch all file open/write activity. Now owned by Microsoft but originally by Sysinternals

share|improve this answer
    
The updated version of this is process monitor. You can get it here:http://technet.microsoft.com/en-us/sysinternals/bb896645 –  yitwail Jun 9 '11 at 19:49

protected by Zypher May 17 '11 at 15:39

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.