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.
 
 

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