FileInfo.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. // Created: Monday, August 13, 2001
  9. //
  10. //------------------------------------------------------------------------------
  11. using System;
  12. using System.PAL;
  13. //using System.Diagnostics;
  14. using System.Security.Permissions;
  15. namespace System.IO
  16. {
  17. /// <summary>
  18. ///
  19. /// </summary>
  20. public sealed class FileInfo : FileSystemInfo
  21. {
  22. private OpSys _os = Platform.OS;
  23. public FileInfo (string fileName)
  24. {
  25. CheckArgument.Path (fileName, false);
  26. //LAMESPEC: Does not throw security exception in constructor
  27. OriginalPath = fileName;
  28. }
  29. private bool existsOnDisk (bool exNotFound, bool exIsDirectory)
  30. {
  31. bool bRetCode;
  32. try
  33. {
  34. Refresh ();
  35. if ((getAttributes () & FileAttributes.Directory) != 0)
  36. {
  37. if (exIsDirectory)
  38. {
  39. throw new UnauthorizedAccessException ();
  40. }
  41. bRetCode = false;
  42. }
  43. else
  44. {
  45. bRetCode = true;
  46. }
  47. }
  48. catch (ArgumentException)
  49. {
  50. if (exNotFound)
  51. {
  52. throw new FileNotFoundException ();
  53. }
  54. bRetCode = false;
  55. }
  56. return bRetCode;
  57. }
  58. public override bool Exists
  59. {
  60. get
  61. {
  62. return existsOnDisk (false, false);
  63. }
  64. }
  65. public override string Name
  66. {
  67. get
  68. {
  69. return Path.GetFileName (getPathName ());
  70. }
  71. }
  72. /// <summary>
  73. /// Gets the parent directory info
  74. /// </summary>
  75. public DirectoryInfo Directory
  76. {
  77. get
  78. {
  79. return new DirectoryInfo (Path.GetDirectoryName (getPathName ()));
  80. }
  81. }
  82. /// <summary>
  83. /// Get the path of the file
  84. /// </summary>
  85. public string DirectoryName
  86. {
  87. get
  88. {
  89. return Path.GetDirectoryName (getPathName ());
  90. }
  91. }
  92. /// <summary>
  93. /// Get the length of the file
  94. /// </summary>
  95. public long Length
  96. {
  97. get
  98. {
  99. try
  100. {
  101. Refresh ();
  102. }
  103. catch (ArgumentException)
  104. {
  105. throw new FileNotFoundException ();
  106. }
  107. return _os.FileLength (getPathName ());
  108. }
  109. }
  110. [MonoTODO]
  111. public StreamWriter AppendText ()
  112. {
  113. return File.AppendText (OriginalPath);
  114. }
  115. public FileStream Create ()
  116. {
  117. return File.Create (OriginalPath);
  118. }
  119. [MonoTODO]
  120. public StreamWriter CreateText ()
  121. {
  122. return new StreamWriter (Open (FileMode.Create, FileAccess.Write));
  123. }
  124. public FileStream Open (FileMode mode)
  125. {
  126. return Open (mode, FileAccess.ReadWrite);
  127. }
  128. public FileStream Open (FileMode mode, FileAccess access)
  129. {
  130. return Open (mode, access, FileShare.None);
  131. }
  132. public FileStream Open (FileMode mode, FileAccess access, FileShare share)
  133. {
  134. bool bExists = existsOnDisk (false, true); // throw is directory;
  135. string path = getPathName ();
  136. CheckPermission.ModeAccess (mode, access, path, bExists);
  137. return new FileStream (path, mode, access, share);
  138. }
  139. [MonoTODO]
  140. public FileStream OpenRead ()
  141. {
  142. return Open (FileMode.Open, FileAccess.Read, FileShare.Read);
  143. }
  144. [MonoTODO]
  145. public StreamReader OpenText ()
  146. {
  147. return new StreamReader (Open (FileMode.OpenOrCreate, FileAccess.ReadWrite));
  148. }
  149. public FileStream OpenWrite ()
  150. {
  151. return Open (FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
  152. }
  153. public FileInfo CopyTo (string destFile)
  154. {
  155. return CopyTo (destFile, false);
  156. }
  157. public FileInfo CopyTo (string destFile, bool bOverwrite)
  158. {
  159. File.Copy (OriginalPath, destFile);
  160. return new FileInfo (destFile);
  161. }
  162. public override void Delete ()
  163. {
  164. existsOnDisk (true, true); // throw not found, is directory
  165. CheckPermission.Demand (FileIOPermissionAccess.AllAccess, getPathName ());
  166. _os.DeleteFile (getPathName ());
  167. }
  168. public void MoveTo (string destName)
  169. {
  170. File.Move (OriginalPath, destName);
  171. }
  172. }
  173. }