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.

File.GetCreationTime.cs 8.8 KiB

8 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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.Security;
  23. namespace Alphaleonis.Win32.Filesystem
  24. {
  25. public static partial class File
  26. {
  27. #region GetCreationTime
  28. /// <summary>Gets the creation date and time of the specified file.</summary>
  29. /// <param name="path">The file for which to obtain creation date and time information.</param>
  30. /// <returns>
  31. /// A <see cref="System.DateTime"/> structure set to the creation date and time for the specified file. This value is expressed in
  32. /// local time.
  33. /// </returns>
  34. [SecurityCritical]
  35. public static DateTime GetCreationTime(string path)
  36. {
  37. return GetCreationTimeCore(null, path, false, PathFormat.RelativePath).ToLocalTime();
  38. }
  39. /// <summary>[AlphaFS] Gets the creation date and time of the specified file.</summary>
  40. /// <param name="path">The file for which to obtain creation date and time information.</param>
  41. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  42. /// <returns>
  43. /// A <see cref="System.DateTime"/> structure set to the creation date and time for the specified file. This value is expressed in
  44. /// local time.
  45. /// </returns>
  46. [SecurityCritical]
  47. public static DateTime GetCreationTime(string path, PathFormat pathFormat)
  48. {
  49. return GetCreationTimeCore(null, path, false, pathFormat).ToLocalTime();
  50. }
  51. #region Transactional
  52. /// <summary>[AlphaFS] Gets the creation date and time of the specified file.</summary>
  53. /// <param name="transaction">The transaction.</param>
  54. /// <param name="path">The file for which to obtain creation date and time information.</param>
  55. /// <returns>
  56. /// A <see cref="System.DateTime"/> structure set to the creation date and time for the specified file. This value is expressed in
  57. /// local time.
  58. /// </returns>
  59. [SecurityCritical]
  60. public static DateTime GetCreationTimeTransacted(KernelTransaction transaction, string path)
  61. {
  62. return GetCreationTimeCore(transaction, path, false, PathFormat.RelativePath).ToLocalTime();
  63. }
  64. /// <summary>[AlphaFS] Gets the creation date and time of the specified file.</summary>
  65. /// <param name="transaction">The transaction.</param>
  66. /// <param name="path">The file for which to obtain creation date and time information.</param>
  67. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  68. /// <returns>
  69. /// A <see cref="System.DateTime"/> structure set to the creation date and time for the specified file. This value is expressed in
  70. /// local time.
  71. /// </returns>
  72. [SecurityCritical]
  73. public static DateTime GetCreationTimeTransacted(KernelTransaction transaction, string path, PathFormat pathFormat)
  74. {
  75. return GetCreationTimeCore(transaction, path, false, pathFormat).ToLocalTime();
  76. }
  77. #endregion // Transacted
  78. #endregion
  79. #region GetCreationTimeUtc
  80. /// <summary>Gets the creation date and time, in Coordinated Universal Time (UTC) format, of the specified file.</summary>
  81. /// <param name="path">
  82. /// The file for which to obtain creation date and time information, in Coordinated Universal Time (UTC) format.
  83. /// </param>
  84. /// <returns>
  85. /// A <see cref="System.DateTime"/> structure set to the creation date and time for the specified file. This value is expressed in UTC
  86. /// time.
  87. /// </returns>
  88. [SecurityCritical]
  89. public static DateTime GetCreationTimeUtc(string path)
  90. {
  91. return GetCreationTimeCore(null, path, true, PathFormat.RelativePath);
  92. }
  93. /// <summary>[AlphaFS] Gets the creation date and time, in Coordinated Universal Time (UTC) format, of the specified file.</summary>
  94. /// <param name="path">
  95. /// The file for which to obtain creation date and time information, in Coordinated Universal Time (UTC) format.
  96. /// </param>
  97. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  98. /// <returns>
  99. /// A <see cref="System.DateTime"/> structure set to the creation date and time for the specified file. This value is expressed in UTC
  100. /// time.
  101. /// </returns>
  102. [SecurityCritical]
  103. public static DateTime GetCreationTimeUtc(string path, PathFormat pathFormat)
  104. {
  105. return GetCreationTimeCore(null, path, true, pathFormat);
  106. }
  107. #region Transactional
  108. /// <summary>[AlphaFS] Gets the creation date and time, in Coordinated Universal Time (UTC) format, of the specified file.</summary>
  109. /// <param name="transaction">The transaction.</param>
  110. /// <param name="path">
  111. /// The file for which to obtain creation date and time information, in Coordinated Universal Time (UTC) format.
  112. /// </param>
  113. /// <returns>
  114. /// A <see cref="System.DateTime"/> structure set to the creation date and time for the specified file. This value is expressed in UTC
  115. /// time.
  116. /// </returns>
  117. [SecurityCritical]
  118. public static DateTime GetCreationTimeUtcTransacted(KernelTransaction transaction, string path)
  119. {
  120. return GetCreationTimeCore(transaction, path, true, PathFormat.RelativePath);
  121. }
  122. /// <summary>[AlphaFS] Gets the creation date and time, in Coordinated Universal Time (UTC) format, of the specified file.</summary>
  123. /// <param name="transaction">The transaction.</param>
  124. /// <param name="path">
  125. /// The file for which to obtain creation date and time information, in Coordinated Universal Time (UTC) format.
  126. /// </param>
  127. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  128. /// <returns>
  129. /// A <see cref="System.DateTime"/> structure set to the creation date and time for the specified file. This value is expressed in UTC
  130. /// time.
  131. /// </returns>
  132. [SecurityCritical]
  133. public static DateTime GetCreationTimeUtcTransacted(KernelTransaction transaction, string path, PathFormat pathFormat)
  134. {
  135. return GetCreationTimeCore(transaction, path, true, pathFormat);
  136. }
  137. #endregion // Transacted
  138. #endregion // GetCreationTimeUtc
  139. #region Internal Methods
  140. /// <summary>
  141. /// [AlphaFS] Gets the creation date and time, in Coordinated Universal Time (UTC) or local time, of the specified file or directory.
  142. /// </summary>
  143. /// <param name="transaction">The transaction.</param>
  144. /// <param name="path">The file or directory for which to obtain creation date and time information.</param>
  145. /// <param name="returnUtc">
  146. /// <see langword="true"/> gets the Coordinated Universal Time (UTC), <see langword="false"/> gets the local time.
  147. /// </param>
  148. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  149. /// <returns>
  150. /// A <see cref="System.DateTime"/> structure set to the creation date and time for the specified file or directory. Depending on
  151. /// <paramref name="returnUtc"/> this value is expressed in UTC- or local time.
  152. /// </returns>
  153. [SecurityCritical]
  154. internal static DateTime GetCreationTimeCore(KernelTransaction transaction, string path, bool returnUtc, PathFormat pathFormat)
  155. {
  156. NativeMethods.FILETIME creationTime = GetAttributesExCore<NativeMethods.WIN32_FILE_ATTRIBUTE_DATA>(transaction, path, pathFormat, false).ftCreationTime;
  157. return returnUtc
  158. ? DateTime.FromFileTimeUtc(creationTime)
  159. : DateTime.FromFileTime(creationTime);
  160. }
  161. #endregion // Internal Methods
  162. }
  163. }