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.

Extensions.cs 1.8 KiB

7 years ago
11 months ago
7 years ago
11 months ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Security.AccessControl;
  5. using Alphaleonis.Win32.Filesystem;
  6. namespace NTFSSecurity
  7. {
  8. public static class Extensions
  9. {
  10. public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
  11. {
  12. if (source == null) { throw new ArgumentException(); }
  13. if (action == null) { throw new ArgumentException(); }
  14. foreach (T element in source)
  15. {
  16. action(element);
  17. }
  18. }
  19. public static FileSystemInfo GetParent(this FileSystemInfo item)
  20. {
  21. var parentPath = System.IO.Path.GetDirectoryName(item.FullName);
  22. if (File.Exists(parentPath))
  23. {
  24. return new FileInfo(parentPath);
  25. }
  26. else if (Directory.Exists(parentPath))
  27. {
  28. return new DirectoryInfo(parentPath);
  29. }
  30. else
  31. {
  32. throw new System.IO.FileNotFoundException();
  33. }
  34. }
  35. public static System.IO.FileSystemInfo GetParent(this System.IO.FileSystemInfo item)
  36. {
  37. var parentPath = System.IO.Path.GetDirectoryName(item.FullName);
  38. if (File.Exists(parentPath))
  39. {
  40. return new System.IO.FileInfo(parentPath);
  41. }
  42. else if (Directory.Exists(parentPath))
  43. {
  44. return new System.IO.DirectoryInfo(parentPath);
  45. }
  46. else
  47. {
  48. throw new System.IO.FileNotFoundException();
  49. }
  50. }
  51. public static List<T> GetListOfNames<T>(this Enum source)
  52. {
  53. return Enum.GetValues(typeof(T)).Cast<T>().ToList();
  54. }
  55. }
  56. }