Closing a process child window (e.g. a Word window) accessing a specific file

Scenario (for force deleting a file, can't go for delete on restart):

  • I have a file that is opened in some program, say MS Word. There could be some other files as well opened in MS Word.
  • In my C# program, I need to know what process is accessing that particular file and need to close/release only that particular file from Word. Other files should not be disturbed, so I won't use Process.Kill() method.
  • Given the above scenario, I have done the following:

  • Found the process (instance of Process class) accessing my target file using the code here

  • Found a list of handles held by that process (using NtQuerySystemInformation() method).

  • From the list of handles, I was able to find the particular handle holding my file (using NtQueryObject() method). Tried to call CloseHandle() on this handle but that does not work.
  • Now I can also list down all child window handles from my target process.
  • How do I find that particular window handle that is related to the file handle I found in step 3 above? My plan is to use SendMessage() method with WM_CLOSE message to that particular window so it would be closed and my file would be released.

    One way is to check the file name string in that child window's title and then close that window. But the issue is, a file with the same name could have been opened from some different location.


    I found this very detail post about the subject, maybe it can help you

    http://forum.sysinternals.com/howto-enumerate-handles_topic18892.html

    That post hase some disucussions and related questions here

    http://forum.sysinternals.com/topic19403.html

    链接地址: http://www.djcxy.com/p/40550.html

    上一篇: 如何以编程方式删除文件?

    下一篇: 关闭访问特定文件的进程子窗口(例如Word窗口)