Results 1 to 3 of 3

Thread: How to Unlock a Folder Locked by Another App

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2007
    Posts
    448

    How to Unlock a Folder Locked by Another App

    Hey all.

    I have been researching this problem and am basically coming up empty. I need to do in code what Unlocker essentially does as an app...That is, remove a lock on a folder that has been locked by some other application. For instance, Photoshop will not lock a file name for a file that is opened, but it WILL lock the containing folder path.

    I looked into using a simple lock/unlock example in vb.Net using:
    'Lock:
    Dim fs As FileSystemSecurity = File.GetAccessControl(PathName_edit.Text)

    fs.AddAccessRule(New FileSystemAccessRule(Environment.UserName, _
    FileSystemRights.FullControl, AccessControlType.Deny))

    File.SetAccessControl(PathName_edit.Text, fs)

    '-----------------------------------------------------------------------------
    'Unlock:
    Dim fs As FileSystemSecurity = File.GetAccessControl(PathName_edit.Text)

    fs.RemoveAccessRule(New FileSystemAccessRule(Environment.UserName, _
    FileSystemRights.FullControl, AccessControlType.Deny))

    File.SetAccessControl(PathName_edit.Text, fs)
    Unfortunately, it only works in a limited sense in that so long as you lock the folder yourself, you can unlock it. But it does not seem to be able to steal control from another app that owns the folder. That seems to me to be something a bit more lower level like an API solution or something. So I went to my win32 references and they are weak on NTFS security topics. Googling this problem only brings up the standard approach above, and a slew of links to little apps that do it for you, but nothing on what is actually happening that makes it possible. I get the impression it is not a simple call, but maybe something that uses SendMessage() or SetWindowsHook() as part of the trick to intercept the messages sent between the owner app and the resource.

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2007
    Posts
    448

    Re: How to Unlock a Folder Locked by Another App

    Found and tried this as well, but no go:
    Dim currentUser As Security.Principal.NTAccount = New Security.Principal.NTAccount(Security.Principal.WindowsIdentity.GetCurrent.Name)
    Dim dirInfo As New IO.DirectoryInfo("D:\test")
    Dim dirSecurity As New Security.AccessControl.DirectorySecurity("D:\test", Security.AccessControl.AccessControlSections.Owner)
    dirSecurity.SetOwner(currentUser)
    dirInfo.SetAccessControl(dirSecurity)
    Both of the approaches in my posts execute without errors, but do not do anything noticeable. Which is interesting in that I would think the operations would fail, if the folder is stubbornly owned by another app. That leads me to think that the folder has a weak ownership by that app, or that .Net cannot trap any errors of that kind.
    Last edited by treddie; Apr 20th, 2013 at 03:41 PM.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2007
    Posts
    448

    Re: How to Unlock a Folder Locked by Another App


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