FileSystemInfo.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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-2005 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.Runtime.InteropServices;
  35. using System.Runtime.Serialization;
  36. using System.Security.Permissions;
  37. namespace System.IO {
  38. [Serializable]
  39. [FileIOPermission (SecurityAction.InheritanceDemand, Unrestricted = true)]
  40. #if NET_2_0
  41. [ComVisible (true)]
  42. #endif
  43. public abstract class FileSystemInfo : MarshalByRefObject, ISerializable {
  44. #region Implementation of ISerializable
  45. [ComVisible(false)]
  46. public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
  47. {
  48. info.AddValue ("OriginalPath", OriginalPath, typeof(string));
  49. info.AddValue ("FullPath", FullPath, typeof(string));
  50. }
  51. #endregion Implementation of ISerializable
  52. // public properties
  53. public abstract bool Exists { get; }
  54. public abstract string Name { get; }
  55. public virtual string FullName {
  56. get {
  57. return FullPath;
  58. }
  59. }
  60. public string Extension {
  61. get {
  62. return Path.GetExtension (Name);
  63. }
  64. }
  65. public FileAttributes Attributes {
  66. get {
  67. Refresh (false);
  68. return stat.Attributes;
  69. }
  70. set {
  71. MonoIOError error;
  72. if (!MonoIO.SetFileAttributes (FullName,
  73. value,
  74. out error))
  75. throw MonoIO.GetException (FullName,
  76. error);
  77. Refresh (true);
  78. }
  79. }
  80. public DateTime CreationTime {
  81. get {
  82. Refresh (false);
  83. return DateTime.FromFileTime (stat.CreationTime);
  84. }
  85. set {
  86. long filetime = value.ToFileTime ();
  87. MonoIOError error;
  88. if (!MonoIO.SetFileTime (FullName, filetime,
  89. -1, -1, out error))
  90. throw MonoIO.GetException (FullName,
  91. error);
  92. Refresh (true);
  93. }
  94. }
  95. [ComVisible(false)]
  96. public DateTime CreationTimeUtc {
  97. get {
  98. return CreationTime.ToUniversalTime ();
  99. }
  100. set {
  101. CreationTime = value.ToLocalTime ();
  102. }
  103. }
  104. public DateTime LastAccessTime {
  105. get {
  106. Refresh (false);
  107. return DateTime.FromFileTime (stat.LastAccessTime);
  108. }
  109. set {
  110. long filetime = value.ToFileTime ();
  111. MonoIOError error;
  112. if (!MonoIO.SetFileTime (FullName, -1,
  113. filetime, -1,
  114. out error))
  115. throw MonoIO.GetException (FullName,
  116. error);
  117. Refresh (true);
  118. }
  119. }
  120. [ComVisible(false)]
  121. public DateTime LastAccessTimeUtc {
  122. get {
  123. Refresh (false);
  124. return LastAccessTime.ToUniversalTime ();
  125. }
  126. set {
  127. LastAccessTime = value.ToLocalTime ();
  128. }
  129. }
  130. public DateTime LastWriteTime {
  131. get {
  132. Refresh (false);
  133. return DateTime.FromFileTime (stat.LastWriteTime);
  134. }
  135. set {
  136. long filetime = value.ToFileTime ();
  137. MonoIOError error;
  138. if (!MonoIO.SetFileTime (FullName, -1, -1,
  139. filetime, out error))
  140. throw MonoIO.GetException (FullName,
  141. error);
  142. Refresh (true);
  143. }
  144. }
  145. [ComVisible(false)]
  146. public DateTime LastWriteTimeUtc {
  147. get {
  148. Refresh (false);
  149. return LastWriteTime.ToUniversalTime ();
  150. }
  151. set {
  152. LastWriteTime = value.ToLocalTime ();
  153. }
  154. }
  155. // public methods
  156. public abstract void Delete ();
  157. public void Refresh ()
  158. {
  159. Refresh (true);
  160. }
  161. // protected
  162. protected FileSystemInfo ()
  163. {
  164. this.valid = false;
  165. this.FullPath = null;
  166. }
  167. protected FileSystemInfo (SerializationInfo info, StreamingContext context)
  168. {
  169. if (info == null)
  170. {
  171. throw new ArgumentNullException("info");
  172. }
  173. FullPath = info.GetString("FullPath");
  174. OriginalPath = info.GetString("OriginalPath");
  175. }
  176. protected string FullPath;
  177. protected string OriginalPath;
  178. // internal
  179. internal void Refresh (bool force)
  180. {
  181. if (valid && !force)
  182. return;
  183. MonoIOError error;
  184. MonoIO.GetFileStat (FullName, out stat, out error);
  185. /* Don't throw on error here, too much other
  186. * stuff relies on it not doing so...
  187. */
  188. valid = true;
  189. InternalRefresh ();
  190. }
  191. internal virtual void InternalRefresh ()
  192. {
  193. }
  194. internal void CheckPath (string path)
  195. {
  196. if (path == null)
  197. throw new ArgumentNullException ("path");
  198. if (path.Length == 0)
  199. throw new ArgumentException ("path", Locale.GetText ("Empty path."));
  200. if (path.IndexOfAny (Path.InvalidPathChars) != -1)
  201. throw new ArgumentException ("path", Locale.GetText ("Invalid characters in path."));
  202. }
  203. internal MonoIOStat stat;
  204. internal bool valid;
  205. }
  206. }