Search code examples
vb.netantivirus

How to block access to any file or exe vb.net?


How do I block and unblock a file or executable from being opened permanently during run time in vb.net?


Solution

  • 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(Application.StartupPath & "\quarantine\" & NewTextDoc.Text)
    FSS.AddAccessRule(New FileSystemAccessRule(Environment.UserName, FileSystemRights.FullControl, AccessControlType.Deny))
    File.SetAccessControl(Application.StartupPath & "\quarantine\" & NewTextDoc.Text, FSS)
    

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

    Dim FSS As FileSystemSecurity = File.GetAccessControl(Application.StartupPath & "\quarantine\" & NewTextDoc.Text)
    FSS.RemoveAccessRule(New FileSystemAccessRule(Environment.UserName, FileSystemRights.FullControl, AccessControlType.Deny))
    File.SetAccessControl(Application.StartupPath & "\quarantine\" & NewTextDoc.Text, FSS)
    

    Finally Hope this will help you :)