Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 

72 rader
2.1 KiB

  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. }