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.
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.
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.
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....
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:
Option Explicit
Private i As Integer
Private Sub Form_Load()
i = FreeFile
Open "C:\test\3.txt" For Input As #i
End Sub
Private Sub Form_Unload(Cancel As Integer)
Close #i
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!
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.
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...
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