Page 1 of 1
How to protect folder to be deleted or renamed?
Posted: Thu May 12, 2016 10:07 am
by Kukulkan
Hi, I wonder how to protect a folder that I created using PB against deletion or renaming? It should be protected as long as my software is running.
But(!) all the content in this folder should stay editable like before. So, only for this particular folder and the content needs to be editable by the current user.
Is it sufficient to create a handle on this (eg ExamineDirectory())?
Any idea? At least for Windows? Cross-Plattform would be better
Thanks,
Kukulkan
Re: How to protect folder to be deleted or renamed?
Posted: Thu May 12, 2016 10:27 am
by RSBasic
You can create a new hidden file that you open. Your folder can't be deleted or renamed.
Re: How to protect folder to be deleted or renamed?
Posted: Thu May 12, 2016 10:44 am
by Kukulkan
Hi RSBasic,
nice idea for Windows. Thanks

Now I need a solution for Mac OSX and Linux...
Kukulkan
Re: How to protect folder to be deleted or renamed?
Posted: Thu May 12, 2016 10:50 am
by Dude
Here's a Windows-only way to do it:
Code: Select all
; Prevent a folder being deleted or renamed while our app is running.
tmpdir$=GetTemporaryDirectory()+"zzz\"
lockfile$=tmpdir$+"zzz"
CreateDirectory(tmpdir$)
CreateFile(#PB_Any,lockfile$)
lockedfile=OpenFile_(lockfile$,@ofs.OFSTRUCT,#OF_SHARE_DENY_READ)
MessageRequester("Test","Leave this prompt open, and try to delete/rename:"+#LF$+tmpdir$)
CloseHandle_(lockedfile)
Re: How to protect folder to be deleted or renamed?
Posted: Thu May 12, 2016 11:02 am
by Keya
also for Windows if you use FILE_FLAG_BACKUP_SEMANTICS you can directly call CreateFile on a directory, keeping a handle open to it without requiring any files (i dont know if you need special user permissions for that, apparently SE_BACKUP_NAME privilege using AdjustTokenPrivileges if youre not Admin)
Code: Select all
sPath.s = "c:\temp\deltest"
CreateDirectory(sPath) ;create in case it doesnt exist
hDir = CreateFile_(sPath, #GENERIC_WRITE, #FILE_SHARE_WRITE, 0, #OPEN_EXISTING, #FILE_FLAG_BACKUP_SEMANTICS, #Null)
If hDir
MessageRequester("Test", "Handle now open to " + sPath + " (shouldnt be able to delete)")
Else
MessageRequester("Error", "Couldnt open " + sPath)
EndIf
;CloseHandle_(hDir) ;can delete it after this
A similar but i guess less flexible approach is to use SetCurrentDirectory which also seems to keep a handle open
I cant test Linux/OSX at the moment but can you delete a directory if a process has an open handle to a file within it?
or if you have a little Sleep.app binary running from a directory, can you delete the directory?
Re: How to protect folder to be deleted or renamed?
Posted: Thu May 12, 2016 12:50 pm
by Dude
Keya, that's awesome!
[Edit 1] Doesn't work with root folders though (C:, D:, etc). Only child folders.
[Edit 2] Needs admin rights for folders such as "C:\Program Files\Internet Explorer\".