Imports System
Imports Microsoft.Win32
Imports System.Security.AccessControl
Imports System.Security.Principal
Public Class Example
Public Shared Sub Main()
Dim user As String = Environment.UserDomainName & "\" & Environment.UserName
Dim mSec As New RegistrySecurity()
Dim rule As New RegistryAccessRule(user,RegistryRights.ReadKey,AccessControlType.Allow)
mSec.AddAccessRule(rule)
rule = New RegistryAccessRule(user,RegistryRights.ChangePermissions,AccessControlType.Deny)
mSec.AddAccessRule(rule)
ShowSecurity(mSec)
rule = New RegistryAccessRule(user,RegistryRights.WriteKey,AccessControlType.Allow)
mSec.AddAccessRule(rule)
ShowSecurity(mSec)
End Sub
Private Shared Sub ShowSecurity(ByVal security As RegistrySecurity)
Console.WriteLine(vbCrLf & "Current access rules:" & vbCrLf)
For Each ar As RegistryAccessRule In security.GetAccessRules(True, True, GetType(NTAccount))
Console.WriteLine("User: {0}", ar.IdentityReference)
Console.WriteLine("Type: {0}", ar.AccessControlType)
Console.WriteLine("Rights: {0}", ar.RegistryRights)
Next
End Sub
End Class