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

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