+92 332 4229 857 99ProjectIdeas@Gmail.com

How to Folder Lock and Unlock (Vb.net)



The following code shows you how to lock and unlock the browsed folder.
For this you have to first browse the folder and then lock it. This namespaces is used to achieve this : 
Imports System.Security.AccessControl



Imports System.Security.AccessControl
Imports System.IO

Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click

        With FolderBrowserDialog1
            If .ShowDialog() = DialogResult.OK Then
                TextBox1.Text = .SelectedPath
            End If
        End With

End Sub

Private Sub btnLock_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLock.Click

    Dim fs As FileSystemSecurity = File.GetAccessControl(TextBox1.Text)
    fs.AddAccessRule(New FileSystemAccessRule(Environment.UserName, FileSystemRights.FullControl, AccessControlType.Deny))
    File.SetAccessControl(TextBox1.Text, fs)

End Sub

Private Sub btnUnLock_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUnLock.Click

    Dim fs As FileSystemSecurity = File.GetAccessControl(TextBox1.Text)
    fs.RemoveAccessRule(New FileSystemAccessRule(Environment.UserName, FileSystemRights.FullControl, AccessControlType.Deny))
    File.SetAccessControl(TextBox1.Text, fs)

End Sub


0 comments: