问题
How do I block and unblock a file or executable from being opened permanently during run time in vb.net?
回答1:
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 :)
来源:https://stackoverflow.com/questions/45783202/how-to-block-access-to-any-file-or-exe-vb-net