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

陌路散爱 提交于 2019-12-11 03:36:47

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!