FileInfo.cs 3.2 KB

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