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.
 
 

632 line
40 KiB

  1. /* Copyright (C) 2008-2016 Peter Palotas, Jeffrey Jangli, Alexandr Normuradov
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. using System;
  22. using System.IO;
  23. using System.Security;
  24. namespace Alphaleonis.Win32.Filesystem
  25. {
  26. partial class FileInfo
  27. {
  28. #region CopyTo
  29. #region .NET
  30. /// <summary>Copies an existing file to a new file, disallowing the overwriting of an existing file.
  31. /// <returns>A new <see cref="FileInfo"/> instance with a fully qualified path.</returns>
  32. /// <remarks>
  33. /// <para>Use this method to prevent overwriting of an existing file by default.</para>
  34. /// <para>Whenever possible, avoid using short file names (such as XXXXXX~1.XXX) with this method.</para>
  35. /// <para>If two files have equivalent short file names then this method may fail and raise an exception and/or result in undesirable behavior.</para>
  36. /// </remarks>
  37. /// </summary>
  38. /// <returns>A new <see cref="FileInfo"/> instance with a fully qualified path.</returns>
  39. /// <exception cref="ArgumentException"/>
  40. /// <exception cref="ArgumentNullException"/>
  41. /// <exception cref="DirectoryNotFoundException"/>
  42. /// <exception cref="FileNotFoundException"/>
  43. /// <exception cref="IOException"/>
  44. /// <exception cref="NotSupportedException"/>
  45. /// <exception cref="UnauthorizedAccessException"/>
  46. /// <param name="destinationPath">The name of the new file to copy to.</param>
  47. [SecurityCritical]
  48. public FileInfo CopyTo(string destinationPath)
  49. {
  50. string destinationPathLp;
  51. CopyToMoveToCore(destinationPath, false, CopyOptions.FailIfExists, null, null, null, out destinationPathLp, PathFormat.RelativePath);
  52. return new FileInfo(Transaction, destinationPathLp, PathFormat.LongFullPath);
  53. }
  54. /// <summary>Copies an existing file to a new file, allowing the overwriting of an existing file.
  55. /// <remarks>
  56. /// <para>Use this method to allow or prevent overwriting of an existing file.</para>
  57. /// <para>Whenever possible, avoid using short file names (such as XXXXXX~1.XXX) with this method.</para>
  58. /// <para>If two files have equivalent short file names then this method may fail and raise an exception and/or result in undesirable behavior.</para>
  59. /// </remarks>
  60. /// </summary>
  61. /// <returns>A new <see cref="FileInfo"/> instance with a fully qualified path.</returns>
  62. /// <exception cref="ArgumentException"/>
  63. /// <exception cref="ArgumentNullException"/>
  64. /// <exception cref="DirectoryNotFoundException"/>
  65. /// <exception cref="FileNotFoundException"/>
  66. /// <exception cref="IOException"/>
  67. /// <exception cref="NotSupportedException"/>
  68. /// <exception cref="UnauthorizedAccessException"/>
  69. /// <param name="destinationPath">The name of the new file to copy to.</param>
  70. /// <param name="overwrite"><see langword="true"/> to allow an existing file to be overwritten; otherwise, <see langword="false"/>.</param>
  71. [SecurityCritical]
  72. public FileInfo CopyTo(string destinationPath, bool overwrite)
  73. {
  74. string destinationPathLp;
  75. CopyToMoveToCore(destinationPath, false, overwrite ? CopyOptions.None : CopyOptions.FailIfExists, null, null, null, out destinationPathLp, PathFormat.RelativePath);
  76. return new FileInfo(Transaction, destinationPathLp, PathFormat.LongFullPath);
  77. }
  78. #endregion // .NET
  79. #region AlphaFS
  80. /// <summary>[AlphaFS] Copies an existing file to a new file, disallowing the overwriting of an existing file.
  81. /// <remarks>
  82. /// <para>Use this method to prevent overwriting of an existing file by default.</para>
  83. /// <para>Whenever possible, avoid using short file names (such as XXXXXX~1.XXX) with this method.</para>
  84. /// <para>If two files have equivalent short file names then this method may fail and raise an exception and/or result in undesirable behavior.</para>
  85. /// </remarks>
  86. /// </summary>
  87. /// <returns>A new <see cref="FileInfo"/> instance with a fully qualified path.</returns>
  88. /// <exception cref="ArgumentException"/>
  89. /// <exception cref="ArgumentNullException"/>
  90. /// <exception cref="DirectoryNotFoundException"/>
  91. /// <exception cref="FileNotFoundException"/>
  92. /// <exception cref="IOException"/>
  93. /// <exception cref="NotSupportedException"/>
  94. /// <exception cref="UnauthorizedAccessException"/>
  95. /// <param name="destinationPath">The name of the new file to copy to.</param>
  96. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  97. [SecurityCritical]
  98. public FileInfo CopyTo(string destinationPath, PathFormat pathFormat)
  99. {
  100. string destinationPathLp;
  101. CopyToMoveToCore(destinationPath, false, CopyOptions.FailIfExists, null, null, null, out destinationPathLp, pathFormat);
  102. return new FileInfo(Transaction, destinationPathLp, PathFormat.LongFullPath);
  103. }
  104. /// <summary>[AlphaFS] Copies an existing file to a new file, allowing the overwriting of an existing file.
  105. /// <remarks>
  106. /// <para>Use this method to allow or prevent overwriting of an existing file.</para>
  107. /// <para>Whenever possible, avoid using short file names (such as XXXXXX~1.XXX) with this method.</para>
  108. /// <para>If two files have equivalent short file names then this method may fail and raise an exception and/or result in undesirable behavior.</para>
  109. /// </remarks>
  110. /// </summary>
  111. /// <returns>A new <see cref="FileInfo"/> instance with a fully qualified path.</returns>
  112. /// <exception cref="ArgumentException"/>
  113. /// <exception cref="ArgumentNullException"/>
  114. /// <exception cref="DirectoryNotFoundException"/>
  115. /// <exception cref="FileNotFoundException"/>
  116. /// <exception cref="IOException"/>
  117. /// <exception cref="NotSupportedException"/>
  118. /// <exception cref="UnauthorizedAccessException"/>
  119. /// <param name="destinationPath">The name of the new file to copy to.</param>
  120. /// <param name="overwrite"><see langword="true"/> to allow an existing file to be overwritten; otherwise, <see langword="false"/>.</param>
  121. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  122. [SecurityCritical]
  123. public FileInfo CopyTo(string destinationPath, bool overwrite, PathFormat pathFormat)
  124. {
  125. string destinationPathLp;
  126. CopyToMoveToCore(destinationPath, false, overwrite ? CopyOptions.None : CopyOptions.FailIfExists, null, null, null, out destinationPathLp, pathFormat);
  127. return new FileInfo(Transaction, destinationPathLp, PathFormat.LongFullPath);
  128. }
  129. /// <summary>[AlphaFS] Copies an existing file to a new file, allowing the overwriting of an existing file, <see cref="CopyOptions"/> can be specified.
  130. /// <remarks>
  131. /// <para>Option <see cref="CopyOptions.NoBuffering"/> is recommended for very large file transfers.</para>
  132. /// <para>Use this method to allow or prevent overwriting of an existing file.</para>
  133. /// <para>Whenever possible, avoid using short file names (such as XXXXXX~1.XXX) with this method.</para>
  134. /// <para>If two files have equivalent short file names then this method may fail and raise an exception and/or result in undesirable behavior.</para>
  135. /// </remarks>
  136. /// </summary>
  137. /// <returns>A new <see cref="FileInfo"/> instance with a fully qualified path.</returns>
  138. /// <exception cref="ArgumentException"/>
  139. /// <exception cref="ArgumentNullException"/>
  140. /// <exception cref="DirectoryNotFoundException"/>
  141. /// <exception cref="FileNotFoundException"/>
  142. /// <exception cref="IOException"/>
  143. /// <exception cref="NotSupportedException"/>
  144. /// <exception cref="UnauthorizedAccessException"/>
  145. /// <param name="destinationPath">The name of the new file to copy to.</param>
  146. /// <param name="copyOptions"><see cref="CopyOptions"/> that specify how the file is to be copied.</param>
  147. [SecurityCritical]
  148. public FileInfo CopyTo(string destinationPath, CopyOptions copyOptions)
  149. {
  150. string destinationPathLp;
  151. CopyToMoveToCore(destinationPath, false, copyOptions, null, null, null, out destinationPathLp, PathFormat.RelativePath);
  152. return new FileInfo(Transaction, destinationPathLp, PathFormat.LongFullPath);
  153. }
  154. /// <summary>[AlphaFS] Copies an existing file to a new file, allowing the overwriting of an existing file, <see cref="CopyOptions"/> can be specified.
  155. /// <remarks>
  156. /// <para>Option <see cref="CopyOptions.NoBuffering"/> is recommended for very large file transfers.</para>
  157. /// <para>Use this method to allow or prevent overwriting of an existing file.</para>
  158. /// <para>Whenever possible, avoid using short file names (such as XXXXXX~1.XXX) with this method.</para>
  159. /// <para>If two files have equivalent short file names then this method may fail and raise an exception and/or result in undesirable behavior.</para>
  160. /// </remarks>
  161. /// </summary>
  162. /// <returns>A new <see cref="FileInfo"/> instance with a fully qualified path.</returns>
  163. /// <exception cref="ArgumentException"/>
  164. /// <exception cref="ArgumentNullException"/>
  165. /// <exception cref="DirectoryNotFoundException"/>
  166. /// <exception cref="FileNotFoundException"/>
  167. /// <exception cref="IOException"/>
  168. /// <exception cref="NotSupportedException"/>
  169. /// <exception cref="UnauthorizedAccessException"/>
  170. /// <param name="destinationPath">The name of the new file to copy to.</param>
  171. /// <param name="copyOptions"><see cref="CopyOptions"/> that specify how the file is to be copied.</param>
  172. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  173. [SecurityCritical]
  174. public FileInfo CopyTo(string destinationPath, CopyOptions copyOptions, PathFormat pathFormat)
  175. {
  176. string destinationPathLp;
  177. CopyToMoveToCore(destinationPath, false, copyOptions, null, null, null, out destinationPathLp, pathFormat);
  178. return new FileInfo(Transaction, destinationPathLp, PathFormat.LongFullPath);
  179. }
  180. /// <summary>[AlphaFS] Copies an existing file to a new file, allowing the overwriting of an existing file, <see cref="CopyOptions"/> can be specified.
  181. /// <remarks>
  182. /// <para>Option <see cref="CopyOptions.NoBuffering"/> is recommended for very large file transfers.</para>
  183. /// <para>Use this method to allow or prevent overwriting of an existing file.</para>
  184. /// <para>Whenever possible, avoid using short file names (such as XXXXXX~1.XXX) with this method.</para>
  185. /// <para>If two files have equivalent short file names then this method may fail and raise an exception and/or result in undesirable behavior.</para>
  186. /// </remarks>
  187. /// </summary>
  188. /// <returns>A new <see cref="FileInfo"/> instance with a fully qualified path.</returns>
  189. /// <exception cref="ArgumentException"/>
  190. /// <exception cref="ArgumentNullException"/>
  191. /// <exception cref="DirectoryNotFoundException"/>
  192. /// <exception cref="FileNotFoundException"/>
  193. /// <exception cref="IOException"/>
  194. /// <exception cref="NotSupportedException"/>
  195. /// <exception cref="UnauthorizedAccessException"/>
  196. /// <param name="destinationPath">The name of the new file to copy to.</param>
  197. /// <param name="copyOptions"><see cref="CopyOptions"/> that specify how the file is to be copied.</param>
  198. /// <param name="preserveDates"><see langword="true"/> if original Timestamps must be preserved, <see langword="false"/> otherwise.</param>
  199. [SecurityCritical]
  200. public FileInfo CopyTo(string destinationPath, CopyOptions copyOptions, bool preserveDates)
  201. {
  202. string destinationPathLp;
  203. CopyToMoveToCore(destinationPath, preserveDates, copyOptions, null, null, null, out destinationPathLp, PathFormat.RelativePath);
  204. return new FileInfo(Transaction, destinationPathLp, PathFormat.LongFullPath);
  205. }
  206. /// <summary>[AlphaFS] Copies an existing file to a new file, allowing the overwriting of an existing file, <see cref="CopyOptions"/> can be specified.
  207. /// <remarks>
  208. /// <para>Option <see cref="CopyOptions.NoBuffering"/> is recommended for very large file transfers.</para>
  209. /// <para>Use this method to allow or prevent overwriting of an existing file.</para>
  210. /// <para>Whenever possible, avoid using short file names (such as XXXXXX~1.XXX) with this method.</para>
  211. /// <para>If two files have equivalent short file names then this method may fail and raise an exception and/or result in undesirable behavior.</para>
  212. /// </remarks>
  213. /// </summary>
  214. /// <returns>A new <see cref="FileInfo"/> instance with a fully qualified path.</returns>
  215. /// <exception cref="ArgumentException"/>
  216. /// <exception cref="ArgumentNullException"/>
  217. /// <exception cref="DirectoryNotFoundException"/>
  218. /// <exception cref="FileNotFoundException"/>
  219. /// <exception cref="IOException"/>
  220. /// <exception cref="NotSupportedException"/>
  221. /// <exception cref="UnauthorizedAccessException"/>
  222. /// <param name="destinationPath">The name of the new file to copy to.</param>
  223. /// <param name="copyOptions"><see cref="CopyOptions"/> that specify how the file is to be copied.</param>
  224. /// <param name="preserveDates"><see langword="true"/> if original Timestamps must be preserved, <see langword="false"/> otherwise.</param>
  225. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  226. [SecurityCritical]
  227. public FileInfo CopyTo(string destinationPath, CopyOptions copyOptions, bool preserveDates, PathFormat pathFormat)
  228. {
  229. string destinationPathLp;
  230. CopyToMoveToCore(destinationPath, preserveDates, copyOptions, null, null, null, out destinationPathLp, pathFormat);
  231. return new FileInfo(Transaction, destinationPathLp, PathFormat.LongFullPath);
  232. }
  233. /// <summary>[AlphaFS] Copies an existing file to a new file, allowing the overwriting of an existing file, <see cref="CopyOptions"/> can be specified.
  234. /// <para>and the possibility of notifying the application of its progress through a callback function.</para>
  235. /// <remarks>
  236. /// <para>Option <see cref="CopyOptions.NoBuffering"/> is recommended for very large file transfers.</para>
  237. /// <para>Use this method to allow or prevent overwriting of an existing file.</para>
  238. /// <para>Whenever possible, avoid using short file names (such as XXXXXX~1.XXX) with this method.</para>
  239. /// <para>If two files have equivalent short file names then this method may fail and raise an exception and/or result in undesirable behavior.</para>
  240. /// </remarks>
  241. /// </summary>
  242. /// <returns>A <see cref="CopyMoveResult"/> class with details of the Copy action.</returns>
  243. /// <exception cref="ArgumentException"/>
  244. /// <exception cref="ArgumentNullException"/>
  245. /// <exception cref="DirectoryNotFoundException"/>
  246. /// <exception cref="FileNotFoundException"/>
  247. /// <exception cref="IOException"/>
  248. /// <exception cref="NotSupportedException"/>
  249. /// <exception cref="UnauthorizedAccessException"/>
  250. /// <param name="destinationPath">The name of the new file to copy to.</param>
  251. /// <param name="copyOptions"><see cref="CopyOptions"/> that specify how the file is to be copied.</param>
  252. /// <param name="progressHandler">A callback function that is called each time another portion of the file has been copied. This parameter can be <see langword="null"/>.</param>
  253. /// <param name="userProgressData">The argument to be passed to the callback function. This parameter can be <see langword="null"/>.</param>
  254. [SecurityCritical]
  255. public CopyMoveResult CopyTo(string destinationPath, CopyOptions copyOptions, CopyMoveProgressRoutine progressHandler, object userProgressData)
  256. {
  257. string destinationPathLp;
  258. var cmr = CopyToMoveToCore(destinationPath, false, copyOptions, null, progressHandler, userProgressData, out destinationPathLp, PathFormat.RelativePath);
  259. CopyToMoveToCoreRefresh(destinationPath, destinationPathLp);
  260. return cmr;
  261. }
  262. /// <summary>[AlphaFS] Copies an existing file to a new file, allowing the overwriting of an existing file, <see cref="CopyOptions"/> can be specified.
  263. /// <remarks>
  264. /// <para>Option <see cref="CopyOptions.NoBuffering"/> is recommended for very large file transfers.</para>
  265. /// <para>Use this method to allow or prevent overwriting of an existing file.</para>
  266. /// <para>Whenever possible, avoid using short file names (such as XXXXXX~1.XXX) with this method.</para>
  267. /// <para>If two files have equivalent short file names then this method may fail and raise an exception and/or result in undesirable behavior.</para>
  268. /// </remarks>
  269. /// </summary>
  270. /// <returns>A <see cref="CopyMoveResult"/> class with details of the Copy action.</returns>
  271. /// <exception cref="ArgumentException"/>
  272. /// <exception cref="ArgumentNullException"/>
  273. /// <exception cref="DirectoryNotFoundException"/>
  274. /// <exception cref="FileNotFoundException"/>
  275. /// <exception cref="IOException"/>
  276. /// <exception cref="NotSupportedException"/>
  277. /// <exception cref="UnauthorizedAccessException"/>
  278. /// <param name="destinationPath">The name of the new file to copy to.</param>
  279. /// <param name="copyOptions"><see cref="CopyOptions"/> that specify how the file is to be copied.</param>
  280. /// <param name="progressHandler">A callback function that is called each time another portion of the file has been copied. This parameter can be <see langword="null"/>.</param>
  281. /// <param name="userProgressData">The argument to be passed to the callback function. This parameter can be <see langword="null"/>.</param>
  282. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  283. [SecurityCritical]
  284. public CopyMoveResult CopyTo(string destinationPath, CopyOptions copyOptions, CopyMoveProgressRoutine progressHandler, object userProgressData, PathFormat pathFormat)
  285. {
  286. string destinationPathLp;
  287. var cmr = CopyToMoveToCore(destinationPath, false, copyOptions, null, progressHandler, userProgressData, out destinationPathLp, pathFormat);
  288. CopyToMoveToCoreRefresh(destinationPath, destinationPathLp);
  289. return cmr;
  290. }
  291. /// <summary>[AlphaFS] Copies an existing file to a new file, allowing the overwriting of an existing file, <see cref="CopyOptions"/> can be specified.
  292. /// <para>and the possibility of notifying the application of its progress through a callback function.</para>
  293. /// <remarks>
  294. /// <para>Option <see cref="CopyOptions.NoBuffering"/> is recommended for very large file transfers.</para>
  295. /// <para>Use this method to allow or prevent overwriting of an existing file.</para>
  296. /// <para>Whenever possible, avoid using short file names (such as XXXXXX~1.XXX) with this method.</para>
  297. /// <para>If two files have equivalent short file names then this method may fail and raise an exception and/or result in undesirable behavior.</para>
  298. /// </remarks>
  299. /// </summary>
  300. /// <returns>A <see cref="CopyMoveResult"/> class with details of the Copy action.</returns>
  301. /// <exception cref="ArgumentException"/>
  302. /// <exception cref="ArgumentNullException"/>
  303. /// <exception cref="DirectoryNotFoundException"/>
  304. /// <exception cref="FileNotFoundException"/>
  305. /// <exception cref="IOException"/>
  306. /// <exception cref="NotSupportedException"/>
  307. /// <exception cref="UnauthorizedAccessException"/>
  308. /// <param name="destinationPath">The name of the new file to copy to.</param>
  309. /// <param name="copyOptions"><see cref="CopyOptions"/> that specify how the file is to be copied.</param>
  310. /// <param name="preserveDates"><see langword="true"/> if original Timestamps must be preserved, <see langword="false"/> otherwise.</param>
  311. /// <param name="progressHandler">A callback function that is called each time another portion of the file has been copied. This parameter can be <see langword="null"/>.</param>
  312. /// <param name="userProgressData">The argument to be passed to the callback function. This parameter can be <see langword="null"/>.</param>
  313. [SecurityCritical]
  314. public CopyMoveResult CopyTo(string destinationPath, CopyOptions copyOptions, bool preserveDates, CopyMoveProgressRoutine progressHandler, object userProgressData)
  315. {
  316. string destinationPathLp;
  317. var cmr = CopyToMoveToCore(destinationPath, preserveDates, copyOptions, null, progressHandler, userProgressData, out destinationPathLp, PathFormat.RelativePath);
  318. CopyToMoveToCoreRefresh(destinationPath, destinationPathLp);
  319. return cmr;
  320. }
  321. /// <summary>[AlphaFS] Copies an existing file to a new file, allowing the overwriting of an existing file, <see cref="CopyOptions"/> can be specified.
  322. /// <para>and the possibility of notifying the application of its progress through a callback function.</para>
  323. /// <remarks>
  324. /// <para>Option <see cref="CopyOptions.NoBuffering"/> is recommended for very large file transfers.</para>
  325. /// <para>Use this method to allow or prevent overwriting of an existing file.</para>
  326. /// <para>Whenever possible, avoid using short file names (such as XXXXXX~1.XXX) with this method.</para>
  327. /// <para>If two files have equivalent short file names then this method may fail and raise an exception and/or result in undesirable behavior.</para>
  328. /// </remarks>
  329. /// </summary>
  330. /// <returns>A <see cref="CopyMoveResult"/> class with details of the Copy action.</returns>
  331. /// <exception cref="ArgumentException"/>
  332. /// <exception cref="ArgumentNullException"/>
  333. /// <exception cref="DirectoryNotFoundException"/>
  334. /// <exception cref="FileNotFoundException"/>
  335. /// <exception cref="IOException"/>
  336. /// <exception cref="NotSupportedException"/>
  337. /// <exception cref="UnauthorizedAccessException"/>
  338. /// <param name="destinationPath">The name of the new file to copy to.</param>
  339. /// <param name="copyOptions"><see cref="CopyOptions"/> that specify how the file is to be copied.</param>
  340. /// <param name="preserveDates"><see langword="true"/> if original Timestamps must be preserved, <see langword="false"/> otherwise.</param>
  341. /// <param name="progressHandler">A callback function that is called each time another portion of the file has been copied. This parameter can be <see langword="null"/>.</param>
  342. /// <param name="userProgressData">The argument to be passed to the callback function. This parameter can be <see langword="null"/>.</param>
  343. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  344. [SecurityCritical]
  345. public CopyMoveResult CopyTo(string destinationPath, CopyOptions copyOptions, bool preserveDates, CopyMoveProgressRoutine progressHandler, object userProgressData, PathFormat pathFormat)
  346. {
  347. string destinationPathLp;
  348. var cmr = CopyToMoveToCore(destinationPath, preserveDates, copyOptions, null, progressHandler, userProgressData, out destinationPathLp, pathFormat);
  349. CopyToMoveToCoreRefresh(destinationPath, destinationPathLp);
  350. return cmr;
  351. }
  352. #endregion // AlphaFS
  353. #endregion // CopyTo
  354. #region MoveTo
  355. #region .NET
  356. /// <summary>Moves a specified file to a new location, providing the option to specify a new file name.
  357. /// <remarks>
  358. /// <para>Use this method to prevent overwriting of an existing file by default.</para>
  359. /// <para>This method works across disk volumes.</para>
  360. /// <para>For example, the file c:\MyFile.txt can be moved to d:\public and renamed NewFile.txt.</para>
  361. /// <para>Whenever possible, avoid using short file names (such as XXXXXX~1.XXX) with this method.</para>
  362. /// <para>If two files have equivalent short file names then this method may fail and raise an exception and/or result in undesirable behavior.</para>
  363. /// </remarks>
  364. /// </summary>
  365. /// <returns>A <see cref="CopyMoveResult"/> class with details of the Move action.</returns>
  366. /// <exception cref="ArgumentException"/>
  367. /// <exception cref="ArgumentNullException"/>
  368. /// <exception cref="DirectoryNotFoundException"/>
  369. /// <exception cref="FileNotFoundException"/>
  370. /// <exception cref="IOException"/>
  371. /// <exception cref="NotSupportedException"/>
  372. /// <exception cref="UnauthorizedAccessException"/>
  373. /// <param name="destinationPath">The path to move the file to, which can specify a different file name.</param>
  374. [SecurityCritical]
  375. public CopyMoveResult MoveTo(string destinationPath)
  376. {
  377. string destinationPathLp;
  378. var copyMoveResult = CopyToMoveToCore(destinationPath, false, null, MoveOptions.CopyAllowed, null, null, out destinationPathLp, PathFormat.RelativePath);
  379. CopyToMoveToCoreRefresh(destinationPath, destinationPathLp);
  380. return copyMoveResult;
  381. }
  382. #endregion // .NET
  383. #region AlphaFS
  384. /// <summary>[AlphaFS] Moves a specified file to a new location, providing the option to specify a new file name.
  385. /// <returns><para>Returns a new <see cref="FileInfo"/> instance with a fully qualified path when successfully moved,</para></returns>
  386. /// <remarks>
  387. /// <para>Use this method to prevent overwriting of an existing file by default.</para>
  388. /// <para>This method works across disk volumes.</para>
  389. /// <para>For example, the file c:\MyFile.txt can be moved to d:\public and renamed NewFile.txt.</para>
  390. /// <para>Whenever possible, avoid using short file names (such as XXXXXX~1.XXX) with this method.</para>
  391. /// <para>If two files have equivalent short file names then this method may fail and raise an exception and/or result in undesirable
  392. /// behavior.</para>
  393. /// </remarks>
  394. /// </summary>
  395. /// <exception cref="ArgumentException"/>
  396. /// <exception cref="ArgumentNullException"/>
  397. /// <exception cref="DirectoryNotFoundException"/>
  398. /// <exception cref="FileNotFoundException"/>
  399. /// <exception cref="IOException"/>
  400. /// <exception cref="NotSupportedException"/>
  401. /// <exception cref="UnauthorizedAccessException"/>
  402. /// <param name="destinationPath">The path to move the file to, which can specify a different file name.</param>
  403. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  404. [SecurityCritical]
  405. public FileInfo MoveTo(string destinationPath, PathFormat pathFormat)
  406. {
  407. string destinationPathLp;
  408. CopyToMoveToCore(destinationPath, false, null, MoveOptions.CopyAllowed, null, null, out destinationPathLp, pathFormat);
  409. return new FileInfo(Transaction, destinationPathLp, PathFormat.LongFullPath);
  410. }
  411. /// <summary>[AlphaFS] Moves a specified file to a new location, providing the option to specify a new file name, <see cref="MoveOptions"/> can be specified.
  412. /// <returns><para>Returns a new <see cref="FileInfo"/> instance with a fully qualified path when successfully moved,</para></returns>
  413. /// <remarks>
  414. /// <para>Use this method to allow or prevent overwriting of an existing file.</para>
  415. /// <para>This method works across disk volumes.</para>
  416. /// <para>For example, the file c:\MyFile.txt can be moved to d:\public and renamed NewFile.txt.</para>
  417. /// <para>Whenever possible, avoid using short file names (such as XXXXXX~1.XXX) with this method.</para>
  418. /// <para>If two files have equivalent short file names then this method may fail and raise an exception and/or result in undesirable
  419. /// behavior.</para>
  420. /// </remarks>
  421. /// </summary>
  422. /// <exception cref="ArgumentException"/>
  423. /// <exception cref="ArgumentNullException"/>
  424. /// <exception cref="DirectoryNotFoundException"/>
  425. /// <exception cref="FileNotFoundException"/>
  426. /// <exception cref="IOException"/>
  427. /// <exception cref="NotSupportedException"/>
  428. /// <exception cref="UnauthorizedAccessException"/>
  429. /// <param name="destinationPath">The path to move the file to, which can specify a different file name.</param>
  430. /// <param name="moveOptions"><see cref="MoveOptions"/> that specify how the directory is to be moved. This parameter can be <see langword="null"/>.</param>
  431. [SecurityCritical]
  432. public FileInfo MoveTo(string destinationPath, MoveOptions moveOptions)
  433. {
  434. string destinationPathLp;
  435. CopyToMoveToCore(destinationPath, false, null, moveOptions, null, null, out destinationPathLp, PathFormat.RelativePath);
  436. return new FileInfo(Transaction, destinationPathLp, PathFormat.LongFullPath);
  437. }
  438. /// <summary>[AlphaFS] Moves a specified file to a new location, providing the option to specify a new file name, <see cref="MoveOptions"/> can be specified.
  439. /// <returns><para>Returns a new <see cref="FileInfo"/> instance with a fully qualified path when successfully moved,</para></returns>
  440. /// <remarks>
  441. /// <para>Use this method to allow or prevent overwriting of an existing file.</para>
  442. /// <para>This method works across disk volumes.</para>
  443. /// <para>For example, the file c:\MyFile.txt can be moved to d:\public and renamed NewFile.txt.</para>
  444. /// <para>Whenever possible, avoid using short file names (such as XXXXXX~1.XXX) with this method.</para>
  445. /// <para>If two files have equivalent short file names then this method may fail and raise an exception and/or result in undesirable
  446. /// behavior.</para>
  447. /// </remarks>
  448. /// </summary>
  449. /// <exception cref="ArgumentException"/>
  450. /// <exception cref="ArgumentNullException"/>
  451. /// <exception cref="DirectoryNotFoundException"/>
  452. /// <exception cref="FileNotFoundException"/>
  453. /// <exception cref="IOException"/>
  454. /// <exception cref="NotSupportedException"/>
  455. /// <exception cref="UnauthorizedAccessException"/>
  456. /// <param name="destinationPath">The path to move the file to, which can specify a different file name.</param>
  457. /// <param name="moveOptions"><see cref="MoveOptions"/> that specify how the directory is to be moved. This parameter can be <see langword="null"/>.</param>
  458. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  459. [SecurityCritical]
  460. public FileInfo MoveTo(string destinationPath, MoveOptions moveOptions, PathFormat pathFormat)
  461. {
  462. string destinationPathLp;
  463. CopyToMoveToCore(destinationPath, false, null, moveOptions, null, null, out destinationPathLp, pathFormat);
  464. return new FileInfo(Transaction, destinationPathLp, PathFormat.LongFullPath);
  465. }
  466. /// <summary>[AlphaFS] Moves a specified file to a new location, providing the option to specify a new file name, <see cref="MoveOptions"/> can be specified,
  467. /// <para>and the possibility of notifying the application of its progress through a callback function.</para>
  468. /// <remarks>
  469. /// <para>Use this method to allow or prevent overwriting of an existing file.</para>
  470. /// <para>This method works across disk volumes.</para>
  471. /// <para>For example, the file c:\MyFile.txt can be moved to d:\public and renamed NewFile.txt.</para>
  472. /// <para>Whenever possible, avoid using short file names (such as XXXXXX~1.XXX) with this method.</para>
  473. /// <para>If two files have equivalent short file names then this method may fail and raise an exception and/or result in undesirable behavior.</para>
  474. /// </remarks>
  475. /// </summary>
  476. /// <returns>A <see cref="CopyMoveResult"/> class with the status of the Move action.</returns>
  477. /// <exception cref="ArgumentException"/>
  478. /// <exception cref="ArgumentNullException"/>
  479. /// <exception cref="DirectoryNotFoundException"/>
  480. /// <exception cref="FileNotFoundException"/>
  481. /// <exception cref="IOException"/>
  482. /// <exception cref="NotSupportedException"/>
  483. /// <exception cref="UnauthorizedAccessException"/>
  484. /// <param name="destinationPath">The path to move the file to, which can specify a different file name.</param>
  485. /// <param name="moveOptions"><see cref="MoveOptions"/> that specify how the directory is to be moved. This parameter can be <see langword="null"/>.</param>
  486. /// <param name="progressHandler">A callback function that is called each time another portion of the directory has been moved. This parameter can be <see langword="null"/>.</param>
  487. /// <param name="userProgressData">The argument to be passed to the callback function. This parameter can be <see langword="null"/>.</param>
  488. [SecurityCritical]
  489. public CopyMoveResult MoveTo(string destinationPath, MoveOptions moveOptions, CopyMoveProgressRoutine progressHandler, object userProgressData)
  490. {
  491. string destinationPathLp;
  492. var cmr = CopyToMoveToCore(destinationPath, false, null, moveOptions, progressHandler, userProgressData, out destinationPathLp, PathFormat.RelativePath);
  493. CopyToMoveToCoreRefresh(destinationPath, destinationPathLp);
  494. return cmr;
  495. }
  496. /// <summary>[AlphaFS] Moves a specified file to a new location, providing the option to specify a new file name, <see cref="MoveOptions"/> can be specified.
  497. /// <remarks>
  498. /// <para>Use this method to allow or prevent overwriting of an existing file.</para>
  499. /// <para>This method works across disk volumes.</para>
  500. /// <para>For example, the file c:\MyFile.txt can be moved to d:\public and renamed NewFile.txt.</para>
  501. /// <para>Whenever possible, avoid using short file names (such as XXXXXX~1.XXX) with this method.</para>
  502. /// <para>If two files have equivalent short file names then this method may fail and raise an exception and/or result in undesirable behavior.</para>
  503. /// </remarks>
  504. /// </summary>
  505. /// <returns>A <see cref="CopyMoveResult"/> class with the status of the Move action.</returns>
  506. /// <exception cref="ArgumentException"/>
  507. /// <exception cref="ArgumentNullException"/>
  508. /// <exception cref="DirectoryNotFoundException"/>
  509. /// <exception cref="FileNotFoundException"/>
  510. /// <exception cref="IOException"/>
  511. /// <exception cref="NotSupportedException"/>
  512. /// <exception cref="UnauthorizedAccessException"/>
  513. /// <param name="destinationPath">The path to move the file to, which can specify a different file name.</param>
  514. /// <param name="moveOptions"><see cref="MoveOptions"/> that specify how the directory is to be moved. This parameter can be <see langword="null"/>.</param>
  515. /// <param name="progressHandler">A callback function that is called each time another portion of the directory has been moved. This parameter can be <see langword="null"/>.</param>
  516. /// <param name="userProgressData">The argument to be passed to the callback function. This parameter can be <see langword="null"/>.</param>
  517. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  518. [SecurityCritical]
  519. public CopyMoveResult MoveTo(string destinationPath, MoveOptions moveOptions, CopyMoveProgressRoutine progressHandler, object userProgressData, PathFormat pathFormat)
  520. {
  521. string destinationPathLp;
  522. var cmr = CopyToMoveToCore(destinationPath, false, null, moveOptions, progressHandler, userProgressData, out destinationPathLp, pathFormat);
  523. CopyToMoveToCoreRefresh(destinationPath, destinationPathLp);
  524. return cmr;
  525. }
  526. #endregion // AlphaFS
  527. #endregion // MoveTo
  528. #region Internal Methods
  529. /// <summary>Copy/move an existing file to a new file, allowing the overwriting of an existing file.
  530. /// <remarks>
  531. /// <para>Option <see cref="CopyOptions.NoBuffering"/> is recommended for very large file transfers.</para>
  532. /// <para>Whenever possible, avoid using short file names (such as XXXXXX~1.XXX) with this method.</para>
  533. /// <para>If two files have equivalent short file names then this method may fail and raise an exception and/or result in undesirable behavior.</para>
  534. /// </remarks>
  535. /// </summary>
  536. /// <returns>A <see cref="CopyMoveResult"/> class with the status of the Copy or Move action.</returns>
  537. /// <param name="destinationPath"><para>A full path string to the destination directory</para></param>
  538. /// <param name="preserveDates"><see langword="true"/> if original Timestamps must be preserved, <see langword="false"/> otherwise.</param>
  539. /// <param name="copyOptions"><para>This parameter can be <see langword="null"/>. Use <see cref="CopyOptions"/> to specify how the file is to be copied.</para></param>
  540. /// <param name="moveOptions"><para>This parameter can be <see langword="null"/>. Use <see cref="MoveOptions"/> that specify how the file is to be moved.</para></param>
  541. /// <param name="progressHandler"><para>This parameter can be <see langword="null"/>. A callback function that is called each time another portion of the file has been copied.</para></param>
  542. /// <param name="userProgressData"><para>This parameter can be <see langword="null"/>. The argument to be passed to the callback function.</para></param>
  543. /// <param name="longFullPath">[out] Returns the retrieved long full path.</param>
  544. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  545. /// <exception cref="ArgumentException"/>
  546. /// <exception cref="ArgumentNullException"/>
  547. /// <exception cref="DirectoryNotFoundException"/>
  548. /// <exception cref="IOException"/>
  549. /// <exception cref="NotSupportedException"/>
  550. /// <exception cref="UnauthorizedAccessException"/>
  551. [SecurityCritical]
  552. private CopyMoveResult CopyToMoveToCore(string destinationPath, bool preserveDates, CopyOptions? copyOptions, MoveOptions? moveOptions, CopyMoveProgressRoutine progressHandler, object userProgressData, out string longFullPath, PathFormat pathFormat)
  553. {
  554. var destinationPathLp = Path.GetExtendedLengthPathCore(Transaction, destinationPath, pathFormat, GetFullPathOptions.TrimEnd | GetFullPathOptions.RemoveTrailingDirectorySeparator | GetFullPathOptions.FullCheck);
  555. longFullPath = destinationPathLp;
  556. // Returns false when CopyMoveProgressResult is PROGRESS_CANCEL or PROGRESS_STOP.
  557. return File.CopyMoveCore(false, Transaction, LongFullName, destinationPathLp, preserveDates, copyOptions, moveOptions, progressHandler, userProgressData, null, PathFormat.LongFullPath);
  558. }
  559. private void CopyToMoveToCoreRefresh(string destinationPath, string destinationPathLp)
  560. {
  561. LongFullName = destinationPathLp;
  562. FullPath = Path.GetRegularPathCore(destinationPathLp, GetFullPathOptions.None, false);
  563. OriginalPath = destinationPath;
  564. DisplayPath = Path.GetRegularPathCore(OriginalPath, GetFullPathOptions.None, false);
  565. _name = Path.GetFileName(destinationPathLp, true);
  566. // Flush any cached information about the file.
  567. Reset();
  568. }
  569. #endregion // Internal Methods
  570. }
  571. }