您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

PrivilegeControl.cs 2.1 KiB

7 年前
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using System.Security.AccessControl;
  3. using ProcessPrivileges;
  4. using System.Diagnostics;
  5. namespace Security2
  6. {
  7. public class AdjustPriviledgeException : Exception
  8. {
  9. public AdjustPriviledgeException(string message)
  10. : base(message)
  11. { }
  12. }
  13. public class PrivilegeControl
  14. {
  15. private Process p;
  16. public PrivilegeControl()
  17. {
  18. p = Process.GetCurrentProcess();
  19. }
  20. public PrivilegeAndAttributesCollection GetPrivileges()
  21. {
  22. return p.GetPrivileges();
  23. }
  24. public AdjustPrivilegeResult EnablePrivilege(Privilege privilege)
  25. {
  26. if (p.GetPrivilegeState(privilege) == PrivilegeState.Disabled)
  27. {
  28. AdjustPrivilegeResult result = p.EnablePrivilege(privilege);
  29. return result;
  30. }
  31. else if (p.GetPrivilegeState(privilege) == PrivilegeState.Removed)
  32. {
  33. throw new PrivilegeNotHeldException(privilege.ToString());
  34. }
  35. else if (p.GetPrivilegeState(privilege) == PrivilegeState.Enabled)
  36. {
  37. throw new AdjustPriviledgeException("Priviledge already enabled");
  38. }
  39. else
  40. {
  41. throw new AdjustPriviledgeException("Unknown Error");
  42. }
  43. }
  44. public AdjustPrivilegeResult DisablePrivilege(Privilege privilege)
  45. {
  46. if (p.GetPrivilegeState(privilege) == PrivilegeState.Enabled)
  47. {
  48. AdjustPrivilegeResult result = p.DisablePrivilege(privilege);
  49. return result;
  50. }
  51. else if (p.GetPrivilegeState(privilege) == PrivilegeState.Removed)
  52. {
  53. throw new PrivilegeNotHeldException(privilege.ToString());
  54. }
  55. else if (p.GetPrivilegeState(privilege) == PrivilegeState.Disabled)
  56. {
  57. throw new AdjustPriviledgeException("Priviledge already disabled");
  58. }
  59. else
  60. {
  61. throw new AdjustPriviledgeException("Unknown Error");
  62. }
  63. }
  64. }
  65. }