Is there any way of programmatically finding out if a mapped drive is currently in use? If I try to unmount the drive with net use
, and the drive is being used, it will tell me that the drive is in use and prompt me to take fur开发者_如何学Cther action.
I'd like to be able to unmap drive automatically if it's no longer being used. The system is a Windows XP.
If this is an inhouse app, as a lowtech easy solution I'd suggest downloading the Handle
app from Technet and executing from your process and then your process can read the results and filter per drive to see if any of them are on the drive you're interested in.
Please note, I've not checked that the license terms for Handle
actually allows this usage but they'll pop up the first time you execute it so that you can check them then.
At a high level, you would need to enumerate all open file handles (or rather all open handles, then filter out everything except the file handles) and then look for any file handles whose object name matches the mapped network drive's device name.
It's a lot of low-level Win32 APIs (NtQuerySystemInformation, NtQueryInformationFile, NtQueryObject, etc) but it's definitely doable. If you're going down this road, I would recommend picking up a copy of Windows Internals plus scouring MSDN and Mark Russinovich's blog.
I don't think it's the type of thing you'll get a complete answer to on here, unless perhaps there's an open source project already doing it which can be linked to. For instance, the source for the Sysinternals tools used to be freely available, and can still be found, like this v4.3x version of the FileMon source, which may shed some light.
精彩评论