FileInfo.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 true;
  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 OpenRead () {
  66. return Open (FileMode.Open, FileAccess.Read);
  67. }
  68. public FileStream OpenWrite () {
  69. return Open (FileMode.OpenOrCreate, FileAccess.Write);
  70. }
  71. public FileStream Open (FileMode mode) {
  72. return Open (mode, FileAccess.ReadWrite);
  73. }
  74. public FileStream Open (FileMode mode, FileAccess access) {
  75. return Open (mode, access, FileShare.None);
  76. }
  77. public FileStream Open (FileMode mode, FileAccess access, FileShare share) {
  78. return new FileStream (FullPath, mode, access, share);
  79. }
  80. // file methods
  81. public override void Delete () {
  82. if (!MonoIO.Exists (FullPath)) // a weird MS.NET behaviour
  83. return;
  84. if (!MonoIO.DeleteFile (FullPath))
  85. throw MonoIO.GetException (OriginalPath);
  86. }
  87. public void MoveTo (string dest) {
  88. File.Move (FullPath, dest);
  89. }
  90. public FileInfo CopyTo (string path) {
  91. return CopyTo (path, false);
  92. }
  93. public FileInfo CopyTo (string path, bool overwrite) {
  94. string dest = Path.GetFullPath (path);
  95. File.Copy (FullPath, dest);
  96. return new FileInfo (dest);
  97. }
  98. public override string ToString () {
  99. return OriginalPath;
  100. }
  101. }
  102. }