我正在编写一个作为本地系统帐户运行的 Windows 服务。我正在尝试确保我是否对开始进一步处理的文件具有完全的读/写访问权限。这是我的代码:
Dim FullPath As String
FullPath = "C:\directory\file.txt"
Dim ps As Security.PermissionSet
ps = New Security.PermissionSet(Security.Permissions.PermissionState.Unrestricted)
ps.AddPermission(New Security.Permissions.FileIOPermission(Security.Permissions.FileIOPermissionAccess.AllAccess, FullPath))
ps.AddPermission(New Security.Permissions.FileIOPermission(Security.Permissions.FileIOPermissionAccess.AllAccess, IO.Path.GetDirectoryName(FullPath)))
Try
ps.Demand()
Catch ex As Security.SecurityException
System.Diagnostics.EventLog.WriteEntry("ShopLink", "File " + FullPath + " will not be parsed. " + ex.Message)
Exit Sub
Catch ex As Exception
System.Diagnostics.EventLog.WriteEntry("ShopLink", "File " + FullPath + " will not be parsed. " + ex.Message)
Exit Sub
End Try
然后我将文件的完全访问权限设置为我的服务运行的用户帐户的“拒绝”。执行后,上面的代码不会抛出任何异常,并允许开始文件处理。当服务稍后尝试更改和/或删除文件时,我收到“拒绝访问”异常。
有什么建议么?