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.

7 年之前
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using Alphaleonis.Win32.Filesystem;
  2. using System.Collections.Generic;
  3. using System.Management.Automation;
  4. namespace NTFSSecurity
  5. {
  6. [Cmdlet(VerbsDiagnostic.Test, "Path2")]
  7. [OutputType(typeof(FileInfo), typeof(DirectoryInfo))]
  8. public class TestPath2 : BaseCmdlet
  9. {
  10. private TestPathType pathType = TestPathType.Any;
  11. [Parameter(Position = 1, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
  12. [ValidateNotNullOrEmpty]
  13. [Alias("FullName")]
  14. public string[] Path
  15. {
  16. get { return paths.ToArray(); }
  17. set
  18. {
  19. paths.Clear();
  20. paths.AddRange(value);
  21. }
  22. }
  23. [Parameter(ValueFromPipelineByPropertyName = true)]
  24. public TestPathType PathType
  25. {
  26. get { return pathType; }
  27. set { pathType = value; }
  28. }
  29. protected override void BeginProcessing()
  30. {
  31. base.BeginProcessing();
  32. if (paths.Count == 0)
  33. {
  34. paths = new List<string>() { GetVariableValue("PWD").ToString() };
  35. }
  36. }
  37. protected override void ProcessRecord()
  38. {
  39. foreach (var path in paths)
  40. {
  41. try
  42. {
  43. FileSystemInfo item;
  44. TryGetFileSystemInfo2(path, out item);
  45. if (item == null)
  46. WriteObject(false);
  47. else
  48. {
  49. if (PathType == TestPathType.Any)
  50. WriteObject(true);
  51. else if (PathType == TestPathType.Container & item is DirectoryInfo)
  52. WriteObject(true);
  53. else if (PathType == TestPathType.Leaf & item is FileInfo)
  54. WriteObject(true);
  55. else
  56. WriteObject(false);
  57. }
  58. }
  59. catch (System.IO.FileNotFoundException ex)
  60. {
  61. WriteError(new ErrorRecord(ex, "PathNotFound", ErrorCategory.ObjectNotFound, path));
  62. }
  63. }
  64. }
  65. protected override void EndProcessing()
  66. {
  67. base.EndProcessing();
  68. }
  69. }
  70. public enum TestPathType
  71. {
  72. Any,
  73. Container,
  74. Leaf
  75. }
  76. }