FileInfo.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. using System;
  13. namespace System.IO {
  14. [Serializable]
  15. public sealed class FileInfo : FileSystemInfo {
  16. private bool exists = false;
  17. public FileInfo (string path) {
  18. CheckPath (path);
  19. OriginalPath = path;
  20. FullPath = Path.GetFullPath (path);
  21. exists = File.Exists (path);
  22. }
  23. // public properties
  24. public override bool Exists {
  25. get {
  26. Refresh (false);
  27. if (stat.Attributes == MonoIO.InvalidFileAttributes)
  28. return false;
  29. if ((stat.Attributes & FileAttributes.Directory) != 0)
  30. return false;
  31. return exists;
  32. }
  33. }
  34. public override string Name {
  35. get {
  36. return Path.GetFileName (FullPath);
  37. }
  38. }
  39. public long Length {
  40. get {
  41. if (!Exists)
  42. throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".");
  43. return stat.Length;
  44. }
  45. }
  46. public string DirectoryName {
  47. get {
  48. return Path.GetDirectoryName (FullPath);
  49. }
  50. }
  51. public DirectoryInfo Directory {
  52. get {
  53. return new DirectoryInfo (DirectoryName);
  54. }
  55. }
  56. // streamreader methods
  57. public StreamReader OpenText () {
  58. return new StreamReader (Open (FileMode.Open, FileAccess.Read));
  59. }
  60. public StreamWriter CreateText () {
  61. return new StreamWriter (Open (FileMode.Create, FileAccess.Write));
  62. }
  63. public StreamWriter AppendText () {
  64. return new StreamWriter (Open (FileMode.Append, FileAccess.Write));
  65. }
  66. // filestream methods
  67. public FileStream Create ()
  68. {
  69. return File.Create (FullPath);
  70. }
  71. public FileStream OpenRead () {
  72. return Open (FileMode.Open, FileAccess.Read);
  73. }
  74. public FileStream OpenWrite () {
  75. return Open (FileMode.OpenOrCreate, FileAccess.Write);
  76. }
  77. public FileStream Open (FileMode mode) {
  78. return Open (mode, FileAccess.ReadWrite);
  79. }
  80. public FileStream Open (FileMode mode, FileAccess access) {
  81. return Open (mode, access, FileShare.None);
  82. }
  83. public FileStream Open (FileMode mode, FileAccess access, FileShare share) {
  84. return new FileStream (FullPath, mode, access, share);
  85. }
  86. // file methods
  87. public override void Delete () {
  88. MonoIOError error;
  89. if (!MonoIO.Exists (FullPath, out error)) {
  90. // a weird MS.NET behaviour
  91. return;
  92. }
  93. if (MonoIO.ExistsDirectory (FullPath, out error)) {
  94. throw new UnauthorizedAccessException ("Access to the path \"" + FullPath + "\" is denied.");
  95. }
  96. if (!MonoIO.DeleteFile (FullPath, out error)) {
  97. throw MonoIO.GetException (OriginalPath,
  98. error);
  99. }
  100. }
  101. public void MoveTo (string dest) {
  102. if (dest == null)
  103. throw new ArgumentNullException ();
  104. MonoIOError error;
  105. if (MonoIO.Exists (dest, out error) ||
  106. MonoIO.ExistsDirectory (dest, out error))
  107. throw new IOException ();
  108. File.Move (FullPath, dest);
  109. this.FullPath = Path.GetFullPath (dest);
  110. }
  111. public FileInfo CopyTo (string path) {
  112. return CopyTo (path, false);
  113. }
  114. public FileInfo CopyTo (string path, bool overwrite) {
  115. string dest = Path.GetFullPath (path);
  116. if (overwrite && File.Exists (path))
  117. File.Delete (path);
  118. File.Copy (FullPath, dest);
  119. return new FileInfo (dest);
  120. }
  121. public override string ToString () {
  122. return OriginalPath;
  123. }
  124. }
  125. }