Advertisement
Guest User

Untitled

a guest
Mar 6th, 2015
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. RegistryKey baseKey;
  2.  
  3. if (!String.IsNullOrEmpty(remoteName))
  4. {
  5. baseKey = RegistryKey.OpenRemoteBaseKey(hive, remoteName, regView);
  6. }
  7. else
  8. {
  9. baseKey = RegistryKey.OpenBaseKey(hive, regView);
  10. }
  11.  
  12. var key = baseKey.OpenSubKey(keyPath, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.FullControl);
  13.  
  14. if (key != null)
  15. {
  16. RegistrySecurity rs = key.GetAccessControl();
  17.  
  18. rs.SetOwner(new NTAccount(domainName, userName));
  19.  
  20. if (removeInheretence)
  21. {
  22. // Removes Inheretence
  23. rs.SetAccessRuleProtection(true, false);
  24. }
  25.  
  26. // Gets a list of explicit ACL's And Removes Them
  27. if (removeExplicitPermissions)
  28. {
  29. AuthorizationRuleCollection aclRuleCollection = rs.GetAccessRules(true, false,
  30. typeof(NTAccount));
  31. foreach (RegistryAccessRule aclRule in aclRuleCollection)
  32. {
  33. rs.RemoveAccessRule(aclRule);
  34. }
  35. }
  36.  
  37. rs.SetAccessRule(new RegistryAccessRule(new NTAccount(domainName, userName),
  38. RegistryRights.FullControl, AccessControlType.Allow));
  39. rs.AddAccessRule(new RegistryAccessRule("System", RegistryRights.FullControl,
  40. AccessControlType.Allow));
  41.  
  42. if (administratorFullControl)
  43. {
  44. rs.AddAccessRule(new RegistryAccessRule("Administrators", RegistryRights.FullControl,
  45. AccessControlType.Allow));
  46. }
  47.  
  48. key.SetAccessControl(rs);
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement