FileSystemInfo.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. //
  13. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  14. //
  15. // Permission is hereby granted, free of charge, to any person obtaining
  16. // a copy of this software and associated documentation files (the
  17. // "Software"), to deal in the Software without restriction, including
  18. // without limitation the rights to use, copy, modify, merge, publish,
  19. // distribute, sublicense, and/or sell copies of the Software, and to
  20. // permit persons to whom the Software is furnished to do so, subject to
  21. // the following conditions:
  22. //
  23. // The above copyright notice and this permission notice shall be
  24. // included in all copies or substantial portions of the Software.
  25. //
  26. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  30. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  31. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  32. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  33. //
  34. using System;
  35. using System.Runtime.InteropServices;
  36. using System.Runtime.Serialization;
  37. namespace System.IO {
  38. [Serializable]
  39. public abstract class FileSystemInfo : MarshalByRefObject, ISerializable {
  40. #region Implementation of ISerializable
  41. [ComVisible(false)]
  42. public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
  43. {
  44. info.AddValue ("OriginalPath", OriginalPath, typeof(string));
  45. info.AddValue ("FullPath", FullPath, typeof(string));
  46. }
  47. #endregion Implementation of ISerializable
  48. // public properties
  49. public abstract bool Exists { get; }
  50. public abstract string Name { get; }
  51. public virtual string FullName {
  52. get {
  53. return FullPath;
  54. }
  55. }
  56. public string Extension {
  57. get {
  58. return Path.GetExtension (Name);
  59. }
  60. }
  61. public FileAttributes Attributes {
  62. get {
  63. Refresh (false);
  64. return stat.Attributes;
  65. }
  66. set {
  67. MonoIOError error;
  68. if (!MonoIO.SetFileAttributes (FullName,
  69. value,
  70. out error))
  71. throw MonoIO.GetException (FullName,
  72. error);
  73. Refresh (true);
  74. }
  75. }
  76. public DateTime CreationTime {
  77. get {
  78. Refresh (false);
  79. return DateTime.FromFileTime (stat.CreationTime);
  80. }
  81. set {
  82. long filetime = value.ToFileTime ();
  83. MonoIOError error;
  84. if (!MonoIO.SetFileTime (FullName, filetime,
  85. -1, -1, out error))
  86. throw MonoIO.GetException (FullName,
  87. error);
  88. Refresh (true);
  89. }
  90. }
  91. [ComVisible(false)]
  92. public DateTime CreationTimeUtc {
  93. get {
  94. return CreationTime.ToUniversalTime ();
  95. }
  96. set {
  97. CreationTime = value.ToLocalTime ();
  98. }
  99. }
  100. public DateTime LastAccessTime {
  101. get {
  102. Refresh (false);
  103. return DateTime.FromFileTime (stat.LastAccessTime);
  104. }
  105. set {
  106. long filetime = value.ToFileTime ();
  107. MonoIOError error;
  108. if (!MonoIO.SetFileTime (FullName, -1,
  109. filetime, -1,
  110. out error))
  111. throw MonoIO.GetException (FullName,
  112. error);
  113. Refresh (true);
  114. }
  115. }
  116. [ComVisible(false)]
  117. public DateTime LastAccessTimeUtc {
  118. get {
  119. Refresh (false);
  120. return LastAccessTime.ToUniversalTime ();
  121. }
  122. set {
  123. LastAccessTime = value.ToLocalTime ();
  124. }
  125. }
  126. public DateTime LastWriteTime {
  127. get {
  128. Refresh (false);
  129. return DateTime.FromFileTime (stat.LastWriteTime);
  130. }
  131. set {
  132. long filetime = value.ToFileTime ();
  133. MonoIOError error;
  134. if (!MonoIO.SetFileTime (FullName, -1, -1,
  135. filetime, out error))
  136. throw MonoIO.GetException (FullName,
  137. error);
  138. Refresh (true);
  139. }
  140. }
  141. [ComVisible(false)]
  142. public DateTime LastWriteTimeUtc {
  143. get {
  144. Refresh (false);
  145. return LastWriteTime.ToUniversalTime ();
  146. }
  147. set {
  148. LastWriteTime = value.ToLocalTime ();
  149. }
  150. }
  151. // public methods
  152. public abstract void Delete ();
  153. public void Refresh ()
  154. {
  155. Refresh (true);
  156. }
  157. // protected
  158. protected FileSystemInfo ()
  159. {
  160. this.valid = false;
  161. this.FullPath = null;
  162. }
  163. protected FileSystemInfo (SerializationInfo info, StreamingContext context)
  164. {
  165. if (info == null)
  166. {
  167. throw new ArgumentNullException("info");
  168. }
  169. FullPath = info.GetString("FullPath");
  170. OriginalPath = info.GetString("OriginalPath");
  171. }
  172. protected string FullPath;
  173. protected string OriginalPath;
  174. // internal
  175. internal void Refresh (bool force)
  176. {
  177. if (valid && !force)
  178. return;
  179. MonoIOError error;
  180. MonoIO.GetFileStat (FullName, out stat, out error);
  181. /* Don't throw on error here, too much other
  182. * stuff relies on it not doing so...
  183. */
  184. valid = true;
  185. InternalRefresh ();
  186. }
  187. internal virtual void InternalRefresh ()
  188. {
  189. }
  190. internal void CheckPath (string path)
  191. {
  192. if (path == null)
  193. throw new ArgumentNullException ();
  194. if (path.IndexOfAny (Path.InvalidPathChars) != -1)
  195. throw new ArgumentException ("Invalid characters in path.");
  196. }
  197. internal MonoIOStat stat;
  198. internal bool valid;
  199. }
  200. }