Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

7 lat temu
7 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using Alphaleonis.Win32.Filesystem;
  2. using System;
  3. using System.Management.Automation;
  4. namespace NTFSSecurity
  5. {
  6. [Cmdlet(VerbsCommon.Copy, "Item2", SupportsShouldProcess = true)]
  7. public class CopyItem2 : 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("Copying 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, "Copy File"))
  85. {
  86. ((FileInfo)item).CopyTo(actualDestination, force ? CopyOptions.None : CopyOptions.FailIfExists, PathFormat.RelativePath);
  87. WriteVerbose(string.Format("File '{0}' copied to '{0}'", resolvedPath, destination));
  88. }
  89. }
  90. else
  91. {
  92. if (ShouldProcess(resolvedPath, "Copy Directory"))
  93. {
  94. ((DirectoryInfo)item).CopyTo(actualDestination, force ? CopyOptions.None : CopyOptions.FailIfExists, PathFormat.RelativePath);
  95. WriteVerbose(string.Format("Directory '{0}' copied 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, "CopyError", ErrorCategory.InvalidData, resolvedPath));
  104. }
  105. catch (Exception ex)
  106. {
  107. WriteError(new ErrorRecord(ex, "CopyError", ErrorCategory.NotSpecified, resolvedPath));
  108. }
  109. }
  110. }
  111. protected override void EndProcessing()
  112. {
  113. base.EndProcessing();
  114. }
  115. }
  116. }