You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FileSystemOwner.cs 1.5 KiB

7 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using Alphaleonis.Win32.Filesystem;
  2. using System.Security.AccessControl;
  3. using System.Security.Principal;
  4. namespace Security2
  5. {
  6. public class FileSystemOwner
  7. {
  8. private FileSystemInfo item;
  9. private IdentityReference2 owner;
  10. private FileSystemSecurity sd;
  11. public FileSystemInfo Item
  12. {
  13. get { return item; }
  14. }
  15. public IdentityReference2 Owner
  16. {
  17. get { return owner; }
  18. }
  19. private FileSystemOwner(FileSystemInfo item, IdentityReference2 owner)
  20. {
  21. this.item = item;
  22. this.owner = owner;
  23. }
  24. public static FileSystemOwner GetOwner(FileSystemSecurity2 sd)
  25. {
  26. return new FileSystemOwner(sd.Item, sd.SecurityDescriptor.GetOwner(typeof(SecurityIdentifier)));
  27. }
  28. public static void SetOwner(FileSystemSecurity2 sd, IdentityReference2 account)
  29. {
  30. sd.SecurityDescriptor.SetOwner(account);
  31. }
  32. public static FileSystemOwner GetOwner(FileSystemInfo item)
  33. {
  34. return GetOwner(new FileSystemSecurity2(item, AccessControlSections.Owner));
  35. }
  36. public static void SetOwner(FileSystemInfo item, IdentityReference2 account)
  37. {
  38. var sd = new FileSystemSecurity2(item, AccessControlSections.Owner);
  39. SetOwner(sd, account);
  40. sd.Write();
  41. }
  42. public override string ToString()
  43. {
  44. return item.FullName;
  45. }
  46. }
  47. }