FileInfo.cs 3.0 KB

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