防止文件被删除但仍然可以读取它

时间:2016-09-06 01:24:26

标签: vb.net

我已经搜索了很多但仍然无法找到有用的东西基本上我尝试更改文件权限以拒绝删除选项并继续修改和许多其他组合。我想要一些东西来防止文件被删除但是仍允许我阅读它我手动和使用vb.net这样做这是我使用的代码(它不是我的顺便说一句) 假设我已经创建了一个文本文件我希望该文件被读取但不被用户删除

Imports System.IO
Imports System.Security.AccessControl
Public Class Form1

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim filename As String = "C:\users\mbpc\desktop\test.txt"
    AddFileSecurity(filename, "Everyone", FileSystemRights.Delete, AccessControlType.Deny)
End Sub

Sub AddFileSecurity(ByVal fileName As String, ByVal account As String, _
    ByVal rights As FileSystemRights, ByVal controlType As AccessControlType)
    Dim fSecurity As FileSecurity = File.GetAccessControl(fileName)
    Dim accessRule As FileSystemAccessRule = _
        New FileSystemAccessRule(account, rights, controlType)
    fSecurity.AddAccessRule(accessRule)
    File.SetAccessControl(fileName, fSecurity)
End Sub

Sub RemoveFileSecurity(ByVal fileName As String, ByVal account As String, _
    ByVal rights As FileSystemRights, ByVal controlType As AccessControlType)
    Dim fSecurity As FileSecurity = File.GetAccessControl(fileName)
    fSecurity.RemoveAccessRule(New FileSystemAccessRule(account, _
        rights, controlType))
    File.SetAccessControl(fileName, fSecurity)
End Sub

End Class

0 个答案:

没有答案