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