Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

MoveItem2.cs 4.4 KiB

il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using Alphaleonis.Win32.Filesystem;
  2. using System;
  3. using System.Management.Automation;
  4. namespace NTFSSecurity
  5. {
  6. [Cmdlet(VerbsCommon.Move, "Item2", SupportsShouldProcess = true)]
  7. public class MoveItem2 : BaseCmdlet
  8. {
  9. private string destination;
  10. private SwitchParameter force;
  11. private bool passThru;
  12. [Parameter(Position = 1, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
  13. [ValidateNotNullOrEmpty]
  14. [Alias("FullName")]
  15. public string[] Path
  16. {
  17. get { return paths.ToArray(); }
  18. set
  19. {
  20. paths.Clear();
  21. paths.AddRange(value);
  22. }
  23. }
  24. [Parameter(Position = 2, Mandatory = true, ValueFromPipelineByPropertyName = true)]
  25. public string Destination
  26. {
  27. get { return destination; }
  28. set { destination = value; }
  29. }
  30. [Parameter]
  31. public SwitchParameter Force
  32. {
  33. get { return force; }
  34. set { force = value; }
  35. }
  36. [Parameter]
  37. public bool PassThru
  38. {
  39. get { return passThru; }
  40. set { passThru = value; }
  41. }
  42. protected override void BeginProcessing()
  43. {
  44. base.BeginProcessing();
  45. destination = GetRelativePath(destination);
  46. WriteVerbose(string.Format("Destination path is '{0}'", destination));
  47. }
  48. protected override void ProcessRecord()
  49. {
  50. foreach (var path in paths)
  51. {
  52. WriteVerbose(string.Format("Moving item '{0}'", path));
  53. FileSystemInfo item = null;
  54. var actualDestination = string.Empty;
  55. var resolvedPath = GetRelativePath(path);
  56. try
  57. {
  58. item = GetFileSystemInfo2(resolvedPath);
  59. }
  60. catch (System.IO.FileNotFoundException ex)
  61. {
  62. WriteError(new ErrorRecord(ex, "FileNotFound", ErrorCategory.ObjectNotFound, resolvedPath));
  63. return;
  64. }
  65. //destination is a directory
  66. if (Directory.Exists(destination))
  67. {
  68. //hence adding the file name to the destination path
  69. actualDestination = System.IO.Path.Combine(destination, System.IO.Path.GetFileName(resolvedPath));
  70. }
  71. else
  72. {
  73. actualDestination = destination;
  74. }
  75. if (!force & File.Exists(actualDestination))
  76. {
  77. WriteError(new ErrorRecord(new AlreadyExistsException(), "DestinationFileAlreadyExists", ErrorCategory.ResourceExists, actualDestination));
  78. return;
  79. }
  80. try
  81. {
  82. if (item is FileInfo)
  83. {
  84. if (ShouldProcess(resolvedPath, "Move File"))
  85. {
  86. ((FileInfo)item).MoveTo(actualDestination, force ? MoveOptions.ReplaceExisting : MoveOptions.CopyAllowed, PathFormat.RelativePath);
  87. WriteVerbose(string.Format("File '{0}' moved to '{0}'", resolvedPath, destination));
  88. }
  89. }
  90. else
  91. {
  92. if (ShouldProcess(resolvedPath, "Move Directory"))
  93. {
  94. ((DirectoryInfo)item).MoveTo(actualDestination, force ? MoveOptions.ReplaceExisting : MoveOptions.CopyAllowed, PathFormat.RelativePath);
  95. WriteVerbose(string.Format("Directory '{0}' moved to '{0}'", resolvedPath, destination));
  96. }
  97. }
  98. if (passThru)
  99. WriteObject(item);
  100. }
  101. catch (System.IO.IOException ex)
  102. {
  103. WriteError(new ErrorRecord(ex, "MoveError", ErrorCategory.InvalidData, resolvedPath));
  104. }
  105. catch (Exception ex)
  106. {
  107. WriteError(new ErrorRecord(ex, "MoveError", ErrorCategory.NotSpecified, resolvedPath));
  108. }
  109. }
  110. }
  111. protected override void EndProcessing()
  112. {
  113. base.EndProcessing();
  114. }
  115. }
  116. }