FileInfo.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 ex)
  49. {
  50. Debug.WriteLine(ex); // eliminates not used warning
  51. if(exNotFound)
  52. {
  53. throw new FileNotFoundException();
  54. }
  55. bRetCode = false;
  56. }
  57. return bRetCode;
  58. }
  59. public override bool Exists
  60. {
  61. get
  62. {
  63. return existsOnDisk(false, false);
  64. }
  65. }
  66. public override string Name
  67. {
  68. get
  69. {
  70. return Path.GetFileName(getPathName());
  71. }
  72. }
  73. /// <summary>
  74. /// Gets the parent directory info
  75. /// </summary>
  76. public DirectoryInfo Directory
  77. {
  78. get
  79. {
  80. return new DirectoryInfo(Path.GetDirectoryName(getPathName()));
  81. }
  82. }
  83. /// <summary>
  84. /// Get the path of the file
  85. /// </summary>
  86. public string DirectoryName
  87. {
  88. get
  89. {
  90. return Path.GetDirectoryName(getPathName());
  91. }
  92. }
  93. /// <summary>
  94. /// Get the length of the file
  95. /// </summary>
  96. public long Length
  97. {
  98. get
  99. {
  100. try
  101. {
  102. Refresh();
  103. }
  104. catch(ArgumentException ex)
  105. {
  106. Debug.WriteLine(ex); // eliminates not used compiler warning
  107. throw new FileNotFoundException();
  108. }
  109. return _os.FileLength(getPathName());
  110. }
  111. }
  112. public StreamWriter AppendText()
  113. { // TODO: verify using correct FileMode here might be Create & Append
  114. return new StreamWriter(Open(FileMode.Append, FileAccess.Write));
  115. }
  116. public FileStream Create()
  117. {
  118. // TODO: verify using correct FileMode here
  119. return Open(FileMode.OpenOrCreate, FileAccess.ReadWrite);
  120. }
  121. public StreamWriter CreateText()
  122. { //TODO: According to doc even CreateText throws a file not found ex
  123. // sounds suspicious so i'll have to check it out later
  124. //existsOnDisk(true, true); // throw not found, is directory
  125. return new StreamWriter(Open(FileMode.Create, FileAccess.Write));
  126. }
  127. public FileStream Open(FileMode mode)
  128. {
  129. return Open(mode, FileAccess.ReadWrite);
  130. }
  131. public FileStream Open(FileMode mode, FileAccess access)
  132. {
  133. return Open(mode, access, FileShare.None);
  134. }
  135. public FileStream Open(FileMode mode, FileAccess access, FileShare share)
  136. {
  137. bool bExists = existsOnDisk(false, true); // throw is directory;
  138. string path = getPathName();
  139. CheckPermission.ModeAccess(mode, access, path, bExists);
  140. return new FileStream(path, mode, access, share);
  141. }
  142. public FileStream OpenRead()
  143. { // TODO: find out what default share should be
  144. return Open(FileMode.Open, FileAccess.Read, FileShare.Read);
  145. }
  146. public StreamReader OpenText()
  147. { // TODO: verify mode and access values
  148. return new StreamReader(Open(FileMode.OpenOrCreate, FileAccess.ReadWrite));
  149. }
  150. public FileStream OpenWrite()
  151. {
  152. return Open(FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
  153. }
  154. public FileInfo CopyTo(string destFile)
  155. {
  156. return CopyTo(destFile, false);
  157. }
  158. public FileInfo CopyTo(string destFile, bool bOverwrite)
  159. { // TODO: Implement
  160. return null;
  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. { // TODO: Implement
  170. }
  171. }
  172. }