FileInfo.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //------------------------------------------------------------------------------
  2. //
  3. // System.IO.FileInfo.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. namespace System.IO {
  36. [Serializable]
  37. public sealed class FileInfo : FileSystemInfo {
  38. private bool exists;
  39. public FileInfo (string path) {
  40. CheckPath (path);
  41. OriginalPath = path;
  42. FullPath = Path.GetFullPath (path);
  43. }
  44. internal override void InternalRefresh ()
  45. {
  46. exists = File.Exists (FullPath);
  47. }
  48. // public properties
  49. public override bool Exists {
  50. get {
  51. Refresh (false);
  52. if (stat.Attributes == MonoIO.InvalidFileAttributes)
  53. return false;
  54. if ((stat.Attributes & FileAttributes.Directory) != 0)
  55. return false;
  56. return exists;
  57. }
  58. }
  59. public override string Name {
  60. get {
  61. return Path.GetFileName (FullPath);
  62. }
  63. }
  64. public long Length {
  65. get {
  66. if (!Exists)
  67. throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".");
  68. return stat.Length;
  69. }
  70. }
  71. public string DirectoryName {
  72. get {
  73. return Path.GetDirectoryName (FullPath);
  74. }
  75. }
  76. public DirectoryInfo Directory {
  77. get {
  78. return new DirectoryInfo (DirectoryName);
  79. }
  80. }
  81. // streamreader methods
  82. public StreamReader OpenText () {
  83. return new StreamReader (Open (FileMode.Open, FileAccess.Read));
  84. }
  85. public StreamWriter CreateText () {
  86. return new StreamWriter (Open (FileMode.Create, FileAccess.Write));
  87. }
  88. public StreamWriter AppendText () {
  89. return new StreamWriter (Open (FileMode.Append, FileAccess.Write));
  90. }
  91. // filestream methods
  92. public FileStream Create ()
  93. {
  94. return File.Create (FullPath);
  95. }
  96. public FileStream OpenRead () {
  97. return Open (FileMode.Open, FileAccess.Read);
  98. }
  99. public FileStream OpenWrite () {
  100. return Open (FileMode.OpenOrCreate, FileAccess.Write);
  101. }
  102. public FileStream Open (FileMode mode) {
  103. return Open (mode, FileAccess.ReadWrite);
  104. }
  105. public FileStream Open (FileMode mode, FileAccess access) {
  106. return Open (mode, access, FileShare.None);
  107. }
  108. public FileStream Open (FileMode mode, FileAccess access, FileShare share) {
  109. return new FileStream (FullPath, mode, access, share);
  110. }
  111. // file methods
  112. public override void Delete () {
  113. MonoIOError error;
  114. if (!MonoIO.Exists (FullPath, out error)) {
  115. // a weird MS.NET behaviour
  116. return;
  117. }
  118. if (MonoIO.ExistsDirectory (FullPath, out error)) {
  119. throw new UnauthorizedAccessException ("Access to the path \"" + FullPath + "\" is denied.");
  120. }
  121. if (!MonoIO.DeleteFile (FullPath, out error)) {
  122. throw MonoIO.GetException (OriginalPath,
  123. error);
  124. }
  125. }
  126. public void MoveTo (string dest) {
  127. if (dest == null)
  128. throw new ArgumentNullException ();
  129. if (dest == Name || dest == FullName)
  130. return;
  131. MonoIOError error;
  132. if (MonoIO.Exists (dest, out error) ||
  133. MonoIO.ExistsDirectory (dest, out error))
  134. throw new IOException ();
  135. File.Move (FullPath, dest);
  136. this.FullPath = Path.GetFullPath (dest);
  137. }
  138. public FileInfo CopyTo (string path) {
  139. return CopyTo (path, false);
  140. }
  141. public FileInfo CopyTo (string path, bool overwrite) {
  142. string dest = Path.GetFullPath (path);
  143. if (overwrite && File.Exists (path))
  144. File.Delete (path);
  145. File.Copy (FullPath, dest);
  146. return new FileInfo (dest);
  147. }
  148. public override string ToString () {
  149. return OriginalPath;
  150. }
  151. }
  152. }