Results 1 to 13 of 13

Thread: Deny deletion of any directory

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    24

    Deny deletion of any directory

    Hello all,

    My application accesses some files which are stored in some directory as per user's wish and i keep track of the path. I want that whenever any one tries to remove the directory which is to be used in my application they should be denied to do so.

    can ne one help me out with this?

    Thanks & regards.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Deny deletion of any directory

    That is not something you can do from a program (at least not reliably).

    The best way to do that is to change permissions on the folder (using Windows Explorer), so that users do not have the ability to delete/modify the folder, and if apt apply to the contents of the folder too.

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Deny deletion of any directory

    Well, you can't stop the deletion of a folder with VB.

    That would have to be done at the OS level simply because anyone wanting to delete the folder could just stop your program, open up Windows Explorer, and do whatever they want.

    You would need to set folder level permissions.

  4. #4
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Deny deletion of any directory

    Open at least one file in the directory and don't close it until your program closes.

    ie: If I wanted to protect C:\test folder, I'd open a file in that folder.

    vb Code:
    1. Option Explicit
    2.  
    3. Private i As Integer
    4.  
    5. Private Sub Form_Load()
    6.     i = FreeFile
    7.     Open "C:\test\3.txt" For Input As #i
    8. End Sub
    9.  
    10. Private Sub Form_Unload(Cancel As Integer)
    11.     Close #i
    12. End Sub

    It works if you try to delete the C:\test folder. Not sure how reliable it is but it's the only practical idea I could come up with.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    24

    Re: Deny deletion of any directory

    i thought it could be somehow possible thro' vb.

    newaz thx..

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Posts
    24

    Re: Deny deletion of any directory

    @ digirev

    No not like that. Even if my program is not running, the folder should not be deleted. Like wat we have in program files folder....some that sort of funtionality....

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Deny deletion of any directory

    Quote Originally Posted by DigiRev
    Open at least one file in the directory and don't close it until your program closes.

    ie: If I wanted to protect C:\test folder, I'd open a file in that folder.

    vb Code:
    1. Option Explicit
    2.  
    3. Private i As Integer
    4.  
    5. Private Sub Form_Load()
    6.     i = FreeFile
    7.     Open "C:\test\3.txt" For Input As #i
    8. End Sub
    9.  
    10. Private Sub Form_Unload(Cancel As Integer)
    11.     Close #i
    12. End Sub

    It works if you try to delete the C:\test folder. Not sure how reliable it is but it's the only practical idea I could come up with.
    Actually, DigiRev, this is a pretty darn good idea. I just create a test folder, opened up a Notepad file, typed some crap into it, minimized all windows, opened up Windows Explorer and tried to delete the folder. This is what I got!
    Attached Images Attached Images  

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Deny deletion of any directory

    Quote Originally Posted by castle
    @ digirev

    No not like that. Even if my program is not running, the folder should not be deleted. Like wat we have in program files folder....some that sort of funtionality....
    Then, as both si_the_geek and I have said, you can't do this through VB code. You would need to set folder permissions at the OS level.

  9. #9
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Deny deletion of any directory

    Well....

    If you use API, you could open the folder for reading & write (not a file in a folder, but the folder itself), and save the file pointer into registry (or file, or whatever...) and close your program (without closing the file pointer).

    Then when you want to release the folder, you open your program again, read back your settings (file pointer), and close the pointer to the folder.

    This is in theory, I never actually tried it, but it's worth to try it I think...

  10. #10
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Deny deletion of any directory

    Quote Originally Posted by CVMichael
    This is in theory, I never actually tried it, but it's worth to try it I think...
    I've never even seen it tried using Visual Basic, although I must admit, it does sound interesting.

  11. #11
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Deny deletion of any directory

    I just test it, and it did not work as expected, but I still figured it out in another way.

    Opening the folder did not work, apparently it does not really open it, it just returns the pointer, but you can still delete it.

    BUT.... you can create a fake file... an ADS file, so you cannot see it... and it works that way !

    And it works the way I originally thought. You open the file, get the pointer, and close application. The file & foder will be locked.... then to unlock, open app, read pointer, and release the file, therefore releasing the directory...

    Here is the code, put 2 command buttons on the form, named "cmdLock" and "cmdUnlock"
    Code:
    Option Explicit
    
    Private Const OPEN_ALWAYS = 4
    Private Const GENERIC_WRITE = &H40000000
    
    Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    
    Public Function LockDir(ByVal Path As String) As Long
        LockDir = CreateFile(Path & ":dirlock", GENERIC_WRITE, 0, 0, OPEN_ALWAYS, 0, 0)
    End Function
    
    Public Function UnlockDir(ByVal hDir As Long) As Boolean
        Dim Ret As Long
        Ret = CloseHandle(hDir)
        UnlockDir = Ret = 1
    End Function
    
    Private Sub Form_Load()
        On Error Resume Next
        
        MkDir "C:\D1"
        MkDir "C:\D1\D2"
        MkDir "C:\D1\D2\D3"
    End Sub
    
    Private Sub cmdLock_Click()
        Dim Ret As Long
        
        Ret = LockDir("C:\D1\D2\D3")
        
        Debug.Print Ret
        If Ret <> -1 Then SaveSetting App.EXEName, "DirPointer", "Pointer", Ret
    End Sub
    
    Private Sub cmdUnlock_Click()
        Dim hDir As Long
        
        hDir = Val(GetSetting(App.EXEName, "DirPointer", "Pointer", -1))
        
        If hDir <> -1 And hDir <> 0 Then
            If UnlockDir(hDir) Then SaveSetting App.EXEName, "DirPointer", "Pointer", -1
        End If
    End Sub

  12. #12
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Deny deletion of any directory

    Slick!

    You should duplicate your last post in a new thread in the CodeBank.

  13. #13
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Deny deletion of any directory

    OK... I will when I get more free time (I'm at work now)

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