FileSystemInfo.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //------------------------------------------------------------------------------
  2. //
  3. // System.IO.FileSystemInfo.cs
  4. //
  5. // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
  6. //
  7. // Author: Jim Richardson, [email protected]
  8. // Dan Lewis ([email protected])
  9. // Created: Monday, August 13, 2001
  10. //
  11. //------------------------------------------------------------------------------
  12. using System;
  13. namespace System.IO {
  14. [Serializable]
  15. public abstract class FileSystemInfo : MarshalByRefObject {
  16. // public properties
  17. public abstract bool Exists { get; }
  18. public abstract string Name { get; }
  19. public virtual string FullName {
  20. get {
  21. return FullPath;
  22. }
  23. }
  24. public string Extension {
  25. get {
  26. return Path.GetExtension (Name);
  27. }
  28. }
  29. public FileAttributes Attributes {
  30. get {
  31. Refresh (false);
  32. return stat.Attributes;
  33. }
  34. set {
  35. MonoIOError error;
  36. if (!MonoIO.SetFileAttributes (FullName,
  37. value,
  38. out error))
  39. throw MonoIO.GetException (error);
  40. Refresh (true);
  41. }
  42. }
  43. public DateTime CreationTime {
  44. get {
  45. Refresh (false);
  46. return DateTime.FromFileTime (stat.CreationTime);
  47. }
  48. set {
  49. long filetime = value.ToFileTime ();
  50. MonoIOError error;
  51. if (!MonoIO.SetFileTime (FullName, filetime,
  52. -1, -1, out error))
  53. throw MonoIO.GetException (error);
  54. Refresh (true);
  55. }
  56. }
  57. public DateTime CreationTimeUtc {
  58. get {
  59. return CreationTime.ToUniversalTime ();
  60. }
  61. set {
  62. CreationTime = value.ToLocalTime ();
  63. }
  64. }
  65. public DateTime LastAccessTime {
  66. get {
  67. Refresh (false);
  68. return DateTime.FromFileTime (stat.LastAccessTime);
  69. }
  70. set {
  71. long filetime = value.ToFileTime ();
  72. MonoIOError error;
  73. if (!MonoIO.SetFileTime (FullName, -1,
  74. filetime, -1,
  75. out error))
  76. throw MonoIO.GetException (error);
  77. Refresh (true);
  78. }
  79. }
  80. public DateTime LastAccessTimeUtc {
  81. get {
  82. Refresh (false);
  83. return LastAccessTime.ToUniversalTime ();
  84. }
  85. set {
  86. LastAccessTime = value.ToLocalTime ();
  87. }
  88. }
  89. public DateTime LastWriteTime {
  90. get {
  91. Refresh (false);
  92. return DateTime.FromFileTime (stat.LastWriteTime);
  93. }
  94. set {
  95. long filetime = value.ToFileTime ();
  96. MonoIOError error;
  97. if (!MonoIO.SetFileTime (FullName, -1, -1,
  98. filetime, out error))
  99. throw MonoIO.GetException (error);
  100. Refresh (true);
  101. }
  102. }
  103. public DateTime LastWriteTimeUtc {
  104. get {
  105. Refresh (false);
  106. return LastWriteTime.ToUniversalTime ();
  107. }
  108. set {
  109. LastWriteTime = value.ToLocalTime ();
  110. }
  111. }
  112. // public methods
  113. public abstract void Delete ();
  114. public void Refresh ()
  115. {
  116. Refresh (true);
  117. }
  118. // protected
  119. protected FileSystemInfo ()
  120. {
  121. this.valid = false;
  122. this.FullPath = null;
  123. }
  124. protected string FullPath;
  125. protected string OriginalPath;
  126. // internal
  127. internal void Refresh (bool force)
  128. {
  129. if (valid && !force)
  130. return;
  131. MonoIOError error;
  132. MonoIO.GetFileStat (FullName, out stat, out error);
  133. valid = true;
  134. }
  135. internal void CheckPath (string path)
  136. {
  137. if (path == null)
  138. throw new ArgumentNullException ();
  139. if (path.IndexOfAny (Path.InvalidPathChars) != -1)
  140. throw new ArgumentException ("Invalid characters in path.");
  141. }
  142. internal MonoIOStat stat;
  143. internal bool valid;
  144. }
  145. }