article in Tech
programming
USB ejector - make it easy to disconnect a drive.
Windows can't stop your 'Generic volume' device because a program is still using it. Close any programs that might be using the device, and then try again later.
It is not easy to remove a harddrive in windows. The machine will not help you end the tasks that hold the device open. It only tells you to close programs and try again later. This makes the user have to exit applications that hold real work/data...having to save, etc. However, most often it is explorer and the user has no way of knowing this information! Most people rip it out the drive when they get this message. It is tempting and I've had to do it myself....
I use Process Explorer to find the handle keeping the device open. Find the handle, and kill it. This works most times. However, I have also had times when Process Explorer will show no handles open (after a kill) and the device will still not remove cleanly. Forcing me to shutdown to remove a device?! Really?
(screen shot of what I'm talking about)
T
For some reason, in windows I often find myself having issues ejecting removable drives. The small taskbar icon for removable software in windows actually disappears. Making it very hard to perform the required eject procedure....which if you don't do can lead to a corrupt disk.
A wonderful open source solution is available here:
Eject USB disks using C# - The Code Project - C# Programming
It enumerates all of the hard drives and allows you to eject.
However, there is something missing from this great little gem. It doesn't allow you to eject the drive if there are files open on the device. This is a problem with removable drives because there isn't a quick and simple way to figure out which programs have the file open. Making the user have to guess which apps have the file open...close applications and keep trying till it works. Though sometimes this doesn't work because the file handle is left in limbo from a non-responsive system process. Leading the user to have to reboot the machine to properly remove the device!
So, an ejector should give the user a list of processes which are holding the device open. That way the user can go to that application and close it, or kill it from task manager. (I'm annoyed that I even have to think about this...since the ejector built into windows should include this functionality!)
In windows getting a list of open files is not something that comes out of the box. It would be wonderful if a program like lsof existed...there are lsof alternatives. Utilities like process explorer and handle which allow you to list/search file handles open by other processes. However, these are closed source programs so I can't integrate the code into my solution. So off to more searching....
Now I'm looking for code to list processes and file handles so I can integrate it into one simple USB disk ejector.
list open files on disk
Sysinternals Forums: Enumerate opened files
Axcis :: View topic - Active File Names from Process - using NtQuerySystemInformation in Ntdll.dll from C#.
CodeGuru: Examine Information on Windows NT System Level Primitives
CodeGuru: Task Manager Extension
Windows Installer XML (WiX) toolset
Created: 2007-11-29 07:51:04
Modified: 2013-01-01 05:23:26