Search code examples
winapiwmic

How to close a file using wmic


I want to be able to programmatically, forcibly close local open file handles on a Windows Server. This includes files and folders that are opened by users connected remotely via a network shared folder.

I have experimented using sysinternals "Handle" which gives me the list of open files. "Handle" can close files but it fails to close handles opened via a network share.

I can close files/folders using the GUI via computer management->sessions console, so I would imagine that it is possible to do using a "wmic" command. I haven't found any examples so far and haven't been able to figure it out from the wmic doc.

Any help would be appreciated Thanks


Solution

  • You can use the NetFileEnum and NetFileClose APIs to simulate a network error which will entice the system to close out the handles for you.

    There is a sample on Raymond's blog: Forcing a file handle closed when it has been opened remotely.