
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Granting/Revoking Access to a File or Registry Key
|
991
Solution
The code shown in Example 17-17 grants and then revokes the ability to perform
write actions on a registry key.
Example 17-17. Granting and revoking the right to perform write actions on a registry key
public static void GrantRevokeRegKeyRights( )
{
NTAccount user = new NTAccount(@"WRKSTN\ST");
using (RegistryKey regKey = Registry.LocalMachine.OpenSubKey(
@"SOFTWARE\MyCompany\MyApp"))
{
GrantRegKeyRights(regKey, user, RegistryRights.WriteKey,
InheritanceFlags.None, PropagationFlags.None, AccessControlType.Allow);
RevokeRegKeyRights(regKey, user, RegistryRights.WriteKey,
InheritanceFlags.None, PropagationFlags.None,
AccessControlType.Allow)
}
}
public static void GrantRegKeyRights(RegistryKey regKey,
NTAccount user,
RegistryRights rightsFlags,
InheritanceFlags inherFlags,
PropagationFlags propFlags,
AccessControlType actFlags)
{
RegistrySecurity regSecurity = regKey.GetAccessControl( );
RegistryAccessRule rule = new RegistryAccessRule(user, rightsFlags, inherFlags,
propFlags, actFlags);
regSecurity.AddAccessRule(rule);
regKey.SetAccessControl(regSecurity);
}
public static void RevokeRegKeyRights(RegistryKey regKey,
NTAccount user,
RegistryRights rightsFlags,
InheritanceFlags inherFlags,
PropagationFlags propFlags,
AccessControlType ...