Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

8 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using Alphaleonis.Win32.Filesystem;
  2. using System;
  3. using System.Management.Automation;
  4. namespace NTFSSecurity
  5. {
  6. [Cmdlet(VerbsCommon.Remove, "Item2", SupportsShouldProcess = true)]
  7. public class RemoveItem2 : BaseCmdlet
  8. {
  9. private SwitchParameter force;
  10. private SwitchParameter recurse;
  11. private string filter;
  12. private bool passThru;
  13. [Parameter(Position = 1, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
  14. [ValidateNotNullOrEmpty]
  15. [Alias("FullName")]
  16. public string[] Path
  17. {
  18. get { return paths.ToArray(); }
  19. set
  20. {
  21. paths.Clear();
  22. paths.AddRange(value);
  23. }
  24. }
  25. [Parameter]
  26. public SwitchParameter Force
  27. {
  28. get { return force; }
  29. set { force = value; }
  30. }
  31. [Parameter]
  32. public SwitchParameter Recurse
  33. {
  34. get { return recurse; }
  35. set { recurse = value; }
  36. }
  37. [Parameter]
  38. public SwitchParameter PassThur
  39. {
  40. get { return passThru; }
  41. set { passThru = value; }
  42. }
  43. protected override void BeginProcessing()
  44. {
  45. base.BeginProcessing();
  46. }
  47. protected override void ProcessRecord()
  48. {
  49. foreach (var path in paths)
  50. {
  51. FileSystemInfo item = null;
  52. try
  53. {
  54. item = GetFileSystemInfo2(path);
  55. }
  56. catch (System.IO.FileNotFoundException ex)
  57. {
  58. WriteError(new ErrorRecord(ex, "FileNotFound", ErrorCategory.ObjectNotFound, path));
  59. return;
  60. }
  61. try
  62. {
  63. if (item is FileInfo)
  64. {
  65. if (ShouldProcess(item.ToString(), "Remove File"))
  66. {
  67. ((FileInfo)item).Delete(force);
  68. WriteVerbose(string.Format("File '{0}' was removed", item.ToString()));
  69. }
  70. }
  71. else
  72. {
  73. if (ShouldProcess(item.ToString(), "Remove Directory"))
  74. {
  75. ((DirectoryInfo)item).Delete(recurse, force);
  76. WriteVerbose(string.Format("Directory '{0}' was removed", item.ToString()));
  77. }
  78. }
  79. if (passThru)
  80. WriteObject(item);
  81. }
  82. catch (System.IO.IOException ex)
  83. {
  84. WriteError(new ErrorRecord(ex, "DeleteError", ErrorCategory.InvalidData, path));
  85. }
  86. catch (Exception ex)
  87. {
  88. WriteError(new ErrorRecord(ex, "DeleteError", ErrorCategory.NotSpecified, path));
  89. }
  90. }
  91. }
  92. protected override void EndProcessing()
  93. {
  94. base.EndProcessing();
  95. }
  96. }
  97. }