Friday 21 November 2014

How to Lock And Unlock a Folder in VB.NET

How to Lock And Unlock a Folder  in VB.NET:
This code can be used to lock and unlock a selected folder.It mainly used for authentication purpose.

Insert the following Namespace
(a)Imports System.Security.AccessControl
(b)Imports System.IO

Lock Folder Code:
Dim fs AsFileSystemSecurity = File.GetAccessControl("Your Folder Name")
fs.AddAccessRule(NewFileSystemAccessRule(Environment.UserName, FileSystemRights.FullControl, AccessControlType.Deny))

File.SetAccessControl("
Your Folder Name", fs)


UnLock Folder Code:
Dim fs AsFileSystemSecurity = File.GetAccessControl("Your Folder Name")
fs.RemoveAccessRule(NewFileSystemAccessRule(Environment.UserName, FileSystemRights.FullControl, AccessControlType.Deny))

File.SetAccessControl(
"Your Folder Name", fs) 



----
Now create a folder and lock it.
Then you can test it by clicking the folder manumally it will show you a message that you are unable to access the folder. 

For accessing the folder again you need to unlock the folder. 

No comments:

Post a Comment