/src/System.IO.FileSystem/src/System/IO/FileSystemInfo.Windows.cs

https://gitlab.com/0072016/0072016-corefx- · C# · 132 lines · 105 code · 13 blank · 14 comment · 12 complexity · 3807af077bfbb80bbdbf7e0e121ac92c MD5 · raw file

  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Security;
  5. namespace System.IO
  6. {
  7. partial class FileSystemInfo : IFileSystemObject
  8. {
  9. // Cache the file/directory information
  10. private Interop.mincore.WIN32_FILE_ATTRIBUTE_DATA _data;
  11. // Cache any error retrieving the file/directory information
  12. // We use this field in conjunction with the Refresh method which should never throw.
  13. // If we succeed we store a zero, on failure we store the HResult so that we can
  14. // throw an appropriate error when attempting to access the cached info.
  15. private int _dataInitialized = -1;
  16. [SecurityCritical]
  17. internal void Init(ref Interop.mincore.WIN32_FIND_DATA findData)
  18. {
  19. // Copy the information to data
  20. _data.PopulateFrom(ref findData);
  21. _dataInitialized = 0;
  22. }
  23. FileAttributes IFileSystemObject.Attributes
  24. {
  25. get
  26. {
  27. EnsureDataInitialized();
  28. return (FileAttributes)_data.fileAttributes;
  29. }
  30. set
  31. {
  32. FileSystem.Current.SetAttributes(FullPath, value);
  33. _dataInitialized = -1;
  34. }
  35. }
  36. bool IFileSystemObject.Exists
  37. {
  38. get
  39. {
  40. if (_dataInitialized == -1)
  41. Refresh();
  42. if (_dataInitialized != 0)
  43. {
  44. // Refresh was unable to initialize the data.
  45. // We should normally be throwing an exception here,
  46. // but Exists is supposed to return true or false.
  47. return false;
  48. }
  49. return (_data.fileAttributes != -1) && ((this is DirectoryInfo) == ((_data.fileAttributes & Interop.mincore.FileAttributes.FILE_ATTRIBUTE_DIRECTORY) == Interop.mincore.FileAttributes.FILE_ATTRIBUTE_DIRECTORY));
  50. }
  51. }
  52. DateTimeOffset IFileSystemObject.CreationTime
  53. {
  54. get
  55. {
  56. EnsureDataInitialized();
  57. long dt = ((long)(_data.ftCreationTimeHigh) << 32) | ((long)_data.ftCreationTimeLow);
  58. return DateTimeOffset.FromFileTime(dt);
  59. }
  60. set
  61. {
  62. FileSystem.Current.SetCreationTime(FullPath, value, this is DirectoryInfo);
  63. _dataInitialized = -1;
  64. }
  65. }
  66. DateTimeOffset IFileSystemObject.LastAccessTime
  67. {
  68. get
  69. {
  70. EnsureDataInitialized();
  71. long dt = ((long)(_data.ftLastAccessTimeHigh) << 32) | ((long)_data.ftLastAccessTimeLow);
  72. return DateTimeOffset.FromFileTime(dt);
  73. }
  74. set
  75. {
  76. FileSystem.Current.SetLastAccessTime(FullPath, value, (this is DirectoryInfo));
  77. _dataInitialized = -1;
  78. }
  79. }
  80. DateTimeOffset IFileSystemObject.LastWriteTime
  81. {
  82. get
  83. {
  84. EnsureDataInitialized();
  85. long dt = ((long)(_data.ftLastWriteTimeHigh) << 32) | ((long)_data.ftLastWriteTimeLow);
  86. return DateTimeOffset.FromFileTime(dt);
  87. }
  88. set
  89. {
  90. FileSystem.Current.SetLastWriteTime(FullPath, value, (this is DirectoryInfo));
  91. _dataInitialized = -1;
  92. }
  93. }
  94. long IFileSystemObject.Length
  95. {
  96. get
  97. {
  98. EnsureDataInitialized();
  99. return ((long)_data.fileSizeHigh) << 32 | ((long)_data.fileSizeLow & 0xFFFFFFFFL);
  100. }
  101. }
  102. private void EnsureDataInitialized()
  103. {
  104. if (_dataInitialized == -1)
  105. {
  106. _data = new Interop.mincore.WIN32_FILE_ATTRIBUTE_DATA();
  107. Refresh();
  108. }
  109. if (_dataInitialized != 0) // Refresh was unable to initialize the data
  110. throw Win32Marshal.GetExceptionForWin32Error(_dataInitialized, FullPath);
  111. }
  112. void IFileSystemObject.Refresh()
  113. {
  114. // This should not throw, instead we store the result so that we can throw it
  115. // when someone actually accesses a property
  116. _dataInitialized = Win32FileSystem.FillAttributeInfo(FullPath, ref _data, false, false);
  117. }
  118. }
  119. }