How to Lock a Folder in Windows 7 or Above?
Microsoft keeps on upgrading its Windows Operating System consistently to improve User experience and Security.
These new upgrades also keep the associated software industry busy in making changes to their old softwares compatible with new OS upgrades.
The technique in this article uses the old Windows programming concepts of creating a file handler and keeping it open with exclusive access. As long as this handler is open, others cannot use this folder, providing a Lock on folder.
Lock a Folder – Code in Mins with Excel
Yes. It is true that you can develop your own lock a folder application in just 2 minutes.
Create a New Excel File, save it with xlsm format and follow the steps explained below to lock a Folder.
1234567891011121314151617181920212223242526272829303132Option Explicit'Declare Windows Api to Create a File for Exclusive access and Return a HandlePrivate Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal PassZero As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal PassZero As Long) As Long'Function to Close the Handle Created by above functionPrivate Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As LongPrivate Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal Operation As String, ByVal Filename As String, Optional ByVal Parameters As String, Optional ByVal Directory As String, Optional ByVal WindowStyle As Long = vbMinimizedFocus) As Long'Values used inside Create File FunctionPrivate Const GENERIC_ALL = &H1Private Const OPEN_EXISTING = 3Private Const FILE_FLAG_BACKUP_SEMANTICS = &H2000000'Windows Folder Lock Code in VBAPrivate Sub Lock_Folder_Windows()Dim LockFolderPath As String, fHandle As LongLockFolderPath = ThisWorkbook.Sheets(1).Cells(1, 1)If LockFolderPath <> "" Then'Create a File Handle for the Path'CreateFile ( FilePat, Access Rights, File Share Mode (0 - Do not share), Security Attrib, Creation Disposition (3 - Open Existing), Flag_attric, Template File)fHandle = CreateFile(LockFolderPath, GENERIC_ALL, ByVal 0&, _ByVal 0&, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, ByVal 0&)'Save the FileHandle for ReferenceThisWorkbook.Sheets(1).Cells(1, 2) = fHandleEnd If'Process CompletedMsgBox "Folder Locked - Subscribe Email to Get New Code Updates"ShellExecute 0, "Open", "https://officetricks.com/email-subscription-page/"End Sub
- Create a TestFolder in a drive and come out of it. (Assume “D:\Folder_Lock_Testing”)
- Type the Folder name with full path in Sheet1 Cell A1.
- Now, press Alt + F11 to view VB editor.
- Copy this code and Paste it in VB Editor.
- Press F5.
All you need to have is Microsoft Excel with Macro Enabled and little knowledge in Excel. That’s it. You have created a Folder Lock Software on your own.
Windows Folder unLock
Now, minimize Excel and try to open the Folder “D:\Folder_Lock_Testing”. You will get a error message and Windows will not allow you to open the Folder.
There is a limitation to this method. The Folder will be locked only as long as the Excel is executing. Once you close the Excel, the Locked folder will be accessible again or use the below code to unlock by yourself.1234567Private Sub Unlock_Folder()'Get File HandleDim fHandle As LongfHandle = ThisWorkbook.Sheets(1).Cells(1, 2)'Close the HandleCloseHandle fHandleEnd Sub
This is just a beginner level tutorial that explains one of the plenty of methods available to lock a Windows Folder.
Folder Lock VB Code
The code here makes use of Windows API function to create a File handle. The same module can be used in a VB application as well.
If you are familiar with VB applications, then you can develop your own Windows Desktop application that can Lock a Folder in Windows System. Since Locking a Folder corresponds to security, this area needs upgrades with every new Windows release.
Lock a Folder – In Windows – Excel Folder Lock Code