CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2006
    Posts
    109

    How to detect name of application that has locked File/Folder?

    Hello to all!

    Season's Greetings!

    Sorry guys to disturb you in your Holiday mood! Facing a problem in my application and not able to find solution to solve it. I'm working on a project in which I need to delete a few files/folders at the time of Application Unload. Many a times it happens that the files/folders which I'm willing to delete are already "in use" by some other application. So, I would like to Log this situation to application log file.

    I've written a code to detect if File is in Use by other application or not. But can we find the name of application executable file that has locked it? I searched on Google for the same; but couldn't find a solution to get this done in Visual Basic 6.0 (or any other programming language for that matter). Instead, I came to know about a tool called "Unlocker" which does the same. So, certainly what I'm looking for is possible to implement in program; just the thing that I don't have even a slightest clue of how it can be achieved.

    Can you please help me in this issue? How can we achieve this in Visual Basic 6.0? I found some Processes related code on PSC (some of them are quite adanced and listed all Threads/Sub-threads etc). But I couldn't conclude from that information on the application that has locked a certain file or folder.

    Please help me to understand the process behind this. Thanks for taking time and reading/thinking over my query.

    Eagerly waiting for your reply ...

    Regards,

    Ruturaaj.

    ----

    *P.S.: If you need to investigate the above mentioned Unlocker application & want me to give you download link then just let me know & I will post it here; no problem. I don't know if Forum rules will allow me to post a link for third party tool here.

  2. #2
    Join Date
    Aug 2001
    Location
    Stockholm, Sweden
    Posts
    1,664

    Re: How to detect name of application that has locked File/Folder?

    You can use the free utility Handle to list/enum all handles (like opened file objects).

    For many years ago, I posted a question about something like this:
    Enum HANDLEs for current process
    AdaraCD gave a good answer. I never got to implement it because I found out that I didn't actually need to enum handles for the project I was doing (so I never tried to code).

    Another solution is to execute Handle.exe and parse the output (start the process and redirect the stdout with pipes). This is acutally a quite good solution I think, because Handle.exe is probably very good written and you don't have to deal with low-level stuff and O/S compatibility problems.
    Last edited by j0nas; December 23rd, 2007 at 04:35 AM.

  3. #3
    Join Date
    Jun 2006
    Posts
    109

    Re: How to detect name of application that has locked File/Folder?

    Thanks for your reply. I will surely give a look at it. From your comment, I'm afraid if this is something similar to listing running Processes. I've done this and as I mentioned, I couldn't really recognize a handle that is responsible for locking a file. I will surely see how Handle.exe works. Will get back to you with further findings. In case if you (or anybody else can come up with some programming answer to address this issue then most welcome!

  4. #4
    Join Date
    Jun 2006
    Posts
    109

    Re: How to detect name of application that has locked File/Folder?

    WOW! Your Handle.exe tool works really nice!! It gives me correct name of Process and other details. Plus I can close certain handle with the same tool, too! Of course, I'm curious to know how to develop such thing; but nevertheless, this will help me to proceed with my project. One big problem is: Redistribution of this tool with my application!

    It's a MS tool; so it can't be Free for redistribution unless it ties-up either User or Programmer with another MS Technology! Not sure about this.

    Still the hunt for solution on how we can recognize the process that has locked a certain file is ON! Suggestions, Remarks or anything that you feel helpful is highly appreciated ... waiting for your valuable inputs.

    -- Ruturaaj.

  5. #5
    Join Date
    Jun 2006
    Posts
    109

    Re: How to detect name of application that has locked File/Folder?

    Quote Originally Posted by j0nas
    You can use the free utility Handle to list/enum all handles (like opened file objects).
    I'm not able to use "-c" of Handle tool to close a handle. Can you please guide me in this? I read the documentation on MS Website but it's not clear to me.

    I opened a PDF File and run Handle.exe over that PDF file and here was the result:

    AcroRd32.exe pid: 3136 190: D:\My Downloads\Temp\Handle\Handle v3.30.pdf

    As per documentation, -c requires me to pass handle and I must specify Process PID to identify Process. I tried ...

    handle -c 190 3136 [did not work]
    handle -c 3136 190 [did not work]

    What's wrong? Can you please show me the correct way of doing it?

    Waiting for your reply ...

    Regards,

    Ruturaaj.

  6. #6
    Join Date
    Aug 2001
    Location
    Stockholm, Sweden
    Posts
    1,664

    Re: How to detect name of application that has locked File/Folder?

    AcroRd32.exe pid: 3136 190: D:\My Downloads\Temp\Handle\Handle v3.30.pdf
    For above, try:
    handle -c 190 -p 3136

  7. #7
    Join Date
    Aug 2001
    Location
    Stockholm, Sweden
    Posts
    1,664

    Re: How to detect name of application that has locked File/Folder?

    About Handle.exe etc...

    You should probably not close handles with it except in rare conditions, cause it will mess with the state of the application which use the handle in question.

    Anyway, the guy who wrote Handle is Mark Russinovich.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured