Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

255 rader
9.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.IO;
  23. using System.Security;
  24. namespace Alphaleonis.Win32.Filesystem
  25. {
  26. /// <summary>Represents information about a file system entry.
  27. /// <para>This class cannot be inherited.</para>
  28. /// </summary>
  29. [SerializableAttribute]
  30. [SecurityCritical]
  31. public sealed class FileSystemEntryInfo
  32. {
  33. /// <summary>Initializes a new instance of the <see cref="FileSystemEntryInfo"/> class.</summary>
  34. /// <param name="findData">The NativeMethods.WIN32_FIND_DATA structure.</param>
  35. internal FileSystemEntryInfo(NativeMethods.WIN32_FIND_DATA findData)
  36. {
  37. Win32FindData = findData;
  38. }
  39. #region Properties
  40. /// <summary>Gets the 8.3 version of the filename.</summary>
  41. /// <value>the 8.3 version of the filename.</value>
  42. public string AlternateFileName
  43. {
  44. get { return Win32FindData.cAlternateFileName; }
  45. }
  46. /// <summary>Gets the attributes.</summary>
  47. /// <value>The attributes.</value>
  48. public FileAttributes Attributes
  49. {
  50. get { return Win32FindData.dwFileAttributes; }
  51. }
  52. /// <summary>Gets the time this entry was created.</summary>
  53. /// <value>The time this entry was created.</value>
  54. public DateTime CreationTime
  55. {
  56. get { return CreationTimeUtc.ToLocalTime(); }
  57. }
  58. /// <summary>Gets the time, in coordinated universal time (UTC), this entry was created.</summary>
  59. /// <value>The time, in coordinated universal time (UTC), this entry was created.</value>
  60. public DateTime CreationTimeUtc
  61. {
  62. get { return DateTime.FromFileTimeUtc(Win32FindData.ftCreationTime); }
  63. }
  64. /// <summary>Gets the name of the file.</summary>
  65. /// <value>The name of the file.</value>
  66. public string FileName
  67. {
  68. get { return Win32FindData.cFileName; }
  69. }
  70. /// <summary>Gets the size of the file.</summary>
  71. /// <value>The size of the file.</value>
  72. public long FileSize
  73. {
  74. get { return NativeMethods.ToLong(Win32FindData.nFileSizeHigh, Win32FindData.nFileSizeLow); }
  75. }
  76. private string _fullPath;
  77. /// <summary>The full path of the file system object.</summary>
  78. public string FullPath
  79. {
  80. get { return _fullPath; }
  81. set
  82. {
  83. LongFullPath = value;
  84. _fullPath = Path.GetRegularPathCore(LongFullPath, GetFullPathOptions.None, false);
  85. }
  86. }
  87. /// <summary>Gets a value indicating whether this instance is compressed.</summary>
  88. /// <value><see langword="true"/> if this instance is compressed; otherwise, <see langword="false"/>.</value>
  89. /// <remarks>
  90. /// It is not possible to change the compression status of a File object by using the SetAttributes method.
  91. /// Instead, you must actually compress the file using either a compression tool or one of the classes in the <see cref="System.IO.Compression"/> namespace.
  92. /// </remarks>
  93. public bool IsCompressed
  94. {
  95. get { return Attributes != (FileAttributes)(-1) && (Attributes & FileAttributes.Compressed) != 0; }
  96. }
  97. /// <summary>Gets a value indicating whether this instance is hidden, and thus is not included in an ordinary directory listing.</summary>
  98. /// <value><see langword="true"/> if this instance is hidden; otherwise, <see langword="false"/>.</value>
  99. public bool IsHidden
  100. {
  101. get { return Attributes != (FileAttributes)(-1) && (Attributes & FileAttributes.Hidden) != 0; }
  102. }
  103. /// <summary>Gets a value indicating whether this instance represents a directory.</summary>
  104. /// <value><see langword="true"/> if this instance represents a directory; otherwise, <see langword="false"/>.</value>
  105. public bool IsDirectory
  106. {
  107. get { return Attributes != (FileAttributes)(-1) && (Attributes & FileAttributes.Directory) != 0; }
  108. }
  109. /// <summary>Gets a value indicating whether this instance is encrypted (EFS).</summary>
  110. /// <value><see langword="true"/> if this instance is encrypted (EFS); otherwise, <see langword="false"/>.</value>
  111. /// <remarks>
  112. /// For a file, this means that all data in the file is encrypted.
  113. /// For a directory, this means that encryption is the default for newly created files and directories.
  114. /// </remarks>
  115. public bool IsEncrypted
  116. {
  117. get { return Attributes != (FileAttributes) (-1) && (Attributes & FileAttributes.Encrypted) != 0; }
  118. }
  119. /// <summary>Gets a value indicating whether this instance is a mount point.</summary>
  120. /// <value><see langword="true"/> if this instance is a mount point; otherwise, <see langword="false"/>.</value>
  121. public bool IsMountPoint
  122. {
  123. get { return ReparsePointTag == ReparsePointTag.MountPoint; }
  124. }
  125. /// <summary>Gets a value indicating whether this instance is offline. The data of the file is not immediately available.</summary>
  126. /// <value><see langword="true"/> if this instance is offline; otherwise, <see langword="false"/>.</value>
  127. public bool IsOffline
  128. {
  129. get { return Attributes != (FileAttributes)(-1) && (Attributes & FileAttributes.Offline) != 0; }
  130. }
  131. /// <summary>Gets a value indicating whether this instance is read-only.</summary>
  132. /// <value><see langword="true"/> if this instance is read-only; otherwise, <see langword="false"/>.</value>
  133. public bool IsReadOnly
  134. {
  135. get { return Attributes != (FileAttributes)(-1) && (Attributes & FileAttributes.ReadOnly) != 0; }
  136. }
  137. /// <summary>Gets a value indicating whether this instance contains a reparse point, which is a block of user-defined data associated with a file or a directory.</summary>
  138. /// <value><see langword="true"/> if this instance contains a reparse point; otherwise, <see langword="false"/>.</value>
  139. public bool IsReparsePoint
  140. {
  141. get { return Attributes != (FileAttributes)(-1) && (Attributes & FileAttributes.ReparsePoint) != 0; }
  142. }
  143. /// <summary>Gets a value indicating whether this instance is a symbolic link.</summary>
  144. /// <value><see langword="true"/> if this instance is a symbolic link; otherwise, <see langword="false"/>.</value>
  145. public bool IsSymbolicLink
  146. {
  147. get { return ReparsePointTag == ReparsePointTag.SymLink; }
  148. }
  149. /// <summary>Gets the time this entry was last accessed.</summary>
  150. /// <value>The time this entry was last accessed.</value>
  151. public DateTime LastAccessTime
  152. {
  153. get { return LastAccessTimeUtc.ToLocalTime(); }
  154. }
  155. /// <summary>Gets the time, in coordinated universal time (UTC), this entry was last accessed.</summary>
  156. /// <value>The time, in coordinated universal time (UTC), this entry was last accessed.</value>
  157. public DateTime LastAccessTimeUtc
  158. {
  159. get { return DateTime.FromFileTimeUtc(Win32FindData.ftLastAccessTime); }
  160. }
  161. /// <summary>Gets the time this entry was last modified.</summary>
  162. /// <value>The time this entry was last modified.</value>
  163. public DateTime LastWriteTime
  164. {
  165. get { return LastWriteTimeUtc.ToLocalTime(); }
  166. }
  167. /// <summary>Gets the time, in coordinated universal time (UTC), this entry was last modified.</summary>
  168. /// <value>The time, in coordinated universal time (UTC), this entry was last modified.</value>
  169. public DateTime LastWriteTimeUtc
  170. {
  171. get { return DateTime.FromFileTimeUtc(Win32FindData.ftLastWriteTime); }
  172. }
  173. private string _longFullPath;
  174. /// <summary>The full path of the file system object in Unicode (LongPath) format.</summary>
  175. public string LongFullPath
  176. {
  177. get { return _longFullPath; }
  178. private set { _longFullPath = Path.GetLongPathCore(value, GetFullPathOptions.None); }
  179. }
  180. /// <summary>Gets the reparse point tag of this entry.</summary>
  181. /// <value>The reparse point tag of this entry.</value>
  182. public ReparsePointTag ReparsePointTag
  183. {
  184. get { return IsReparsePoint ? Win32FindData.dwReserved0 : ReparsePointTag.None; }
  185. }
  186. /// <summary>Gets internal WIN32 FIND Data</summary>
  187. internal NativeMethods.WIN32_FIND_DATA Win32FindData { get; private set; }
  188. #endregion // Properties
  189. #region Methods
  190. /// <summary>Returns the <see cref="FullPath"/> of the <see cref="FileSystemEntryInfo"/> instance.</summary>
  191. /// <returns>The <see cref="FullPath"/> instance as a string.</returns>
  192. public override string ToString()
  193. {
  194. return FullPath;
  195. }
  196. #endregion // Methods
  197. }
  198. }