25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

109 lines
3.1 KiB

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