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

FileSystemAccessRule2.RemoveFileSystemAccessRules.cs 6.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using Alphaleonis.Win32.Filesystem;
  2. using System.Collections.Generic;
  3. using System.Security.AccessControl;
  4. namespace Security2
  5. {
  6. public partial class FileSystemAccessRule2
  7. {
  8. public static void RemoveFileSystemAccessRule(FileSystemInfo item, IdentityReference2 account, FileSystemRights2 rights, AccessControlType type, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags, bool removeSpecific = false)
  9. {
  10. if (type == AccessControlType.Allow)
  11. rights = rights | FileSystemRights2.Synchronize;
  12. FileSystemAccessRule ace = null;
  13. if (item as FileInfo != null)
  14. {
  15. var file = (FileInfo)item;
  16. var sd = file.GetAccessControl(AccessControlSections.Access);
  17. ace = (FileSystemAccessRule)sd.AccessRuleFactory(account, (int)rights, false, inheritanceFlags, propagationFlags, type);
  18. if (removeSpecific)
  19. sd.RemoveAccessRuleSpecific(ace);
  20. else
  21. sd.RemoveAccessRule(ace);
  22. file.SetAccessControl(sd);
  23. }
  24. else
  25. {
  26. DirectoryInfo directory = (DirectoryInfo)item;
  27. var sd = directory.GetAccessControl(AccessControlSections.Access);
  28. ace = (FileSystemAccessRule)sd.AccessRuleFactory(account, (int)rights, false, inheritanceFlags, propagationFlags, type);
  29. if (removeSpecific)
  30. sd.RemoveAccessRuleSpecific(ace);
  31. else
  32. sd.RemoveAccessRule(ace);
  33. directory.SetAccessControl(sd);
  34. }
  35. }
  36. public static void RemoveFileSystemAccessRule(FileSystemInfo item, List<IdentityReference2> accounts, FileSystemRights2 rights, AccessControlType type, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags, bool removeSpecific = false)
  37. {
  38. foreach (var account in accounts)
  39. {
  40. RemoveFileSystemAccessRule(item, account, rights, type, inheritanceFlags, propagationFlags, removeSpecific);
  41. }
  42. }
  43. public static void RemoveFileSystemAccessRule(string path, IdentityReference2 account, FileSystemRights2 rights, AccessControlType type, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags, bool removeSpecific = false)
  44. {
  45. if (File.Exists(path))
  46. {
  47. var item = new FileInfo(path);
  48. RemoveFileSystemAccessRule(item, account, rights, type, inheritanceFlags, propagationFlags, removeSpecific);
  49. }
  50. else
  51. {
  52. var item = new DirectoryInfo(path);
  53. RemoveFileSystemAccessRule(item, account, rights, type, inheritanceFlags, propagationFlags, removeSpecific);
  54. }
  55. }
  56. public static void RemoveFileSystemAccessRule(string path, List<IdentityReference2> account, FileSystemRights2 rights, AccessControlType type, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags, bool removeSpecific = false)
  57. {
  58. if (File.Exists(path))
  59. {
  60. var item = new FileInfo(path);
  61. RemoveFileSystemAccessRule(item, account, rights, type, inheritanceFlags, propagationFlags, removeSpecific);
  62. }
  63. else
  64. {
  65. var item = new DirectoryInfo(path);
  66. RemoveFileSystemAccessRule(item, account, rights, type, inheritanceFlags, propagationFlags, removeSpecific);
  67. }
  68. }
  69. public static void RemoveFileSystemAccessRule(FileSystemInfo item, FileSystemAccessRule ace, bool removeSpecific = false)
  70. {
  71. if (item as FileInfo != null)
  72. {
  73. var file = (FileInfo)item;
  74. var sd = file.GetAccessControl(AccessControlSections.Access);
  75. if (removeSpecific)
  76. sd.RemoveAccessRuleSpecific(ace);
  77. else
  78. sd.RemoveAccessRule(ace);
  79. file.SetAccessControl(sd);
  80. }
  81. else
  82. {
  83. DirectoryInfo directory = (DirectoryInfo)item;
  84. var sd = directory.GetAccessControl(AccessControlSections.Access);
  85. if (removeSpecific)
  86. sd.RemoveAccessRuleSpecific(ace);
  87. else
  88. sd.RemoveAccessRule(ace);
  89. directory.SetAccessControl(sd);
  90. }
  91. }
  92. public static FileSystemAccessRule2 RemoveFileSystemAccessRule(FileSystemSecurity2 sd, IdentityReference2 account, FileSystemRights2 rights, AccessControlType type, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags, bool removeSpecific = false)
  93. {
  94. if (type == AccessControlType.Allow)
  95. rights = rights | FileSystemRights2.Synchronize;
  96. var ace = (FileSystemAccessRule)sd.SecurityDescriptor.AccessRuleFactory(account, (int)rights, false, inheritanceFlags, propagationFlags, type);
  97. if (sd.IsFile)
  98. {
  99. if (removeSpecific)
  100. ((FileSecurity)sd.SecurityDescriptor).RemoveAccessRuleSpecific(ace);
  101. else
  102. ((FileSecurity)sd.SecurityDescriptor).RemoveAccessRule(ace);
  103. }
  104. else
  105. {
  106. if (removeSpecific)
  107. ((DirectorySecurity)sd.SecurityDescriptor).RemoveAccessRuleSpecific(ace);
  108. else
  109. ((DirectorySecurity)sd.SecurityDescriptor).RemoveAccessRule(ace);
  110. }
  111. return ace;
  112. }
  113. public static IEnumerable<FileSystemAccessRule2> RemoveFileSystemAccessRule(FileSystemSecurity2 sd, List<IdentityReference2> accounts, FileSystemRights2 rights, AccessControlType type, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags, bool removeSpecific = false)
  114. {
  115. var aces = new List<FileSystemAccessRule2>();
  116. foreach (var account in accounts)
  117. {
  118. aces.Add(RemoveFileSystemAccessRule(sd, account, rights, type, inheritanceFlags, propagationFlags));
  119. }
  120. return aces;
  121. }
  122. }
  123. }