Brandon Williams Brandon Williams - 1 month ago 7
Vb.net Question

how to block a access to any file or exe vb.net?

I was wondering if anyone new how i could block a and unblock a file or executable from being opened permanently and to unblock the file using code in vb.net thanks in advance.

Answer Source

You can use this code, and you can use Environment.UserName to get the name of the user, and this is will lock any type of file and it will lock folder too :

Dim FSS As FileSystemSecurity = File.GetAccessControl("FilePath/FolderPath")
FSS.AddAccessRule(New FileSystemAccessRule(Environment.UserName, FileSystemRights.FullControl, AccessControlType.Deny))
File.SetAccessControl("FilePath/FolderPath", FSS)

And to unlock the file/folder just remove the AccessRule like this :

Dim FSS As FileSystemSecurity = File.GetAccessControl("FilePath/FolderPath")
FSS.RemoveAccessRule(New FileSystemAccessRule(Environment.UserName, FileSystemRights.FullControl, AccessControlType.Deny))
File.SetAccessControl("FilePath/FolderPath", FSS)

Finally Hope this will help you :)