How to Lock 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.
Since Locking a Folder corresponds to security, this area needs upgrades with every new Windows release. 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 Folder Lock.
Creating Your Own Simple Folder Lock Software in 2 Mins
Yes. It is true that you can develop your own lock folder application in just 2 minutes. All you need to have is Microsoft Excel with Macro Enabled and little knowledge in Excel.
Create a New Excel File, save it with xlsm format and follow the steps explained below to lock a Folder.
Visual Basic123456789101112131415161718192021222324252627282930313233343536373839Option 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 = &H2000000Private Sub Lock_Folder()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", "http://officetricks.com/email-subscription-page/"End SubPrivate Sub Unlock_Folder()'Get File HandleDim fHandle As LongfHandle = ThisWorkbook.Sheets(1).Cells(1, 2)'Close the HandleCloseHandle fHandleEnd 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.
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.
That’s it. You have created a Folder Lock Software on your own. 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.
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 Folder in Windows System.
Lock a Folder In Windows Using This Simple Excel Macro Trick