FileSystemInfo.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. }
  41. }
  42. public DateTime CreationTime {
  43. get {
  44. Refresh (false);
  45. return DateTime.FromFileTime (stat.CreationTime);
  46. }
  47. set {
  48. long filetime = value.ToFileTime ();
  49. MonoIOError error;
  50. if (!MonoIO.SetFileTime (FullName, filetime,
  51. -1, -1, out error))
  52. throw MonoIO.GetException (error);
  53. }
  54. }
  55. public DateTime LastAccessTime {
  56. get {
  57. Refresh (false);
  58. return DateTime.FromFileTime (stat.LastAccessTime);
  59. }
  60. set {
  61. long filetime = value.ToFileTime ();
  62. MonoIOError error;
  63. if (!MonoIO.SetFileTime (FullName, -1,
  64. filetime, -1,
  65. out error))
  66. throw MonoIO.GetException (error);
  67. }
  68. }
  69. public DateTime LastWriteTime {
  70. get {
  71. Refresh (false);
  72. return DateTime.FromFileTime (stat.LastWriteTime);
  73. }
  74. set {
  75. long filetime = value.ToFileTime ();
  76. MonoIOError error;
  77. if (!MonoIO.SetFileTime (FullName, -1, -1,
  78. filetime, out error))
  79. throw MonoIO.GetException (error);
  80. }
  81. }
  82. // public methods
  83. public abstract void Delete ();
  84. public void Refresh ()
  85. {
  86. Refresh (true);
  87. }
  88. // protected
  89. protected FileSystemInfo ()
  90. {
  91. this.valid = false;
  92. this.FullPath = null;
  93. }
  94. protected string FullPath;
  95. protected string OriginalPath;
  96. // internal
  97. internal void Refresh (bool force)
  98. {
  99. if (valid && !force)
  100. return;
  101. MonoIOError error;
  102. MonoIO.GetFileStat (FullName, out stat, out error);
  103. valid = true;
  104. }
  105. internal void CheckPath (string path)
  106. {
  107. if (path == null)
  108. throw new ArgumentNullException ();
  109. if (path.IndexOfAny (Path.InvalidPathChars) != -1)
  110. throw new ArgumentException ("Invalid characters in path.");
  111. }
  112. internal MonoIOStat stat;
  113. internal bool valid;
  114. }
  115. }