FileInfo.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. //
  13. // Copyright (C) 2004, 2006 Novell, Inc (http://www.novell.com)
  14. //
  15. // Permission is hereby granted, free of charge, to any person obtaining
  16. // a copy of this software and associated documentation files (the
  17. // "Software"), to deal in the Software without restriction, including
  18. // without limitation the rights to use, copy, modify, merge, publish,
  19. // distribute, sublicense, and/or sell copies of the Software, and to
  20. // permit persons to whom the Software is furnished to do so, subject to
  21. // the following conditions:
  22. //
  23. // The above copyright notice and this permission notice shall be
  24. // included in all copies or substantial portions of the Software.
  25. //
  26. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  30. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  31. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  32. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  33. //
  34. using System.Runtime.InteropServices;
  35. #if NET_2_0
  36. using System.Security.AccessControl;
  37. #endif
  38. namespace System.IO {
  39. [Serializable]
  40. #if NET_2_0
  41. [ComVisible (true)]
  42. #endif
  43. public sealed class FileInfo : FileSystemInfo {
  44. private bool exists;
  45. public FileInfo (string path) {
  46. CheckPath (path);
  47. OriginalPath = path;
  48. FullPath = Path.GetFullPath (path);
  49. }
  50. internal override void InternalRefresh ()
  51. {
  52. exists = File.Exists (FullPath);
  53. }
  54. // public properties
  55. public override bool Exists {
  56. get {
  57. Refresh (false);
  58. if (stat.Attributes == MonoIO.InvalidFileAttributes)
  59. return false;
  60. if ((stat.Attributes & FileAttributes.Directory) != 0)
  61. return false;
  62. return exists;
  63. }
  64. }
  65. public override string Name {
  66. get {
  67. return Path.GetFileName (FullPath);
  68. }
  69. }
  70. #if NET_2_0
  71. public bool IsReadOnly {
  72. get {
  73. if (!Exists)
  74. throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
  75. return ((stat.Attributes & FileAttributes.ReadOnly) != 0);
  76. }
  77. set {
  78. if (!Exists)
  79. throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
  80. FileAttributes attrs = File.GetAttributes(FullPath);
  81. if (value)
  82. attrs |= FileAttributes.ReadOnly;
  83. else
  84. attrs &= ~FileAttributes.ReadOnly;
  85. File.SetAttributes(FullPath, attrs);
  86. }
  87. }
  88. [MonoLimitation ("File encryption isn't supported (even on NTFS).")]
  89. [ComVisible (false)]
  90. public void Encrypt ()
  91. {
  92. // MS.NET support this only on NTFS file systems, i.e. it's a file-system (not a framework) feature.
  93. // otherwise it throws a NotSupportedException (or a PlatformNotSupportedException on older OS).
  94. // we throw the same (instead of a NotImplementedException) because most code should already be
  95. // handling this exception to work properly.
  96. throw new NotSupportedException (Locale.GetText ("File encryption isn't supported on any file system."));
  97. }
  98. [MonoLimitation ("File encryption isn't supported (even on NTFS).")]
  99. [ComVisible (false)]
  100. public void Decrypt ()
  101. {
  102. // MS.NET support this only on NTFS file systems, i.e. it's a file-system (not a framework) feature.
  103. // otherwise it throws a NotSupportedException (or a PlatformNotSupportedException on older OS).
  104. // we throw the same (instead of a NotImplementedException) because most code should already be
  105. // handling this exception to work properly.
  106. throw new NotSupportedException (Locale.GetText ("File encryption isn't supported on any file system."));
  107. }
  108. #endif
  109. public long Length {
  110. get {
  111. if (!Exists)
  112. throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
  113. return stat.Length;
  114. }
  115. }
  116. public string DirectoryName {
  117. get {
  118. return Path.GetDirectoryName (FullPath);
  119. }
  120. }
  121. public DirectoryInfo Directory {
  122. get {
  123. return new DirectoryInfo (DirectoryName);
  124. }
  125. }
  126. // streamreader methods
  127. public StreamReader OpenText () {
  128. return new StreamReader (Open (FileMode.Open, FileAccess.Read));
  129. }
  130. public StreamWriter CreateText () {
  131. return new StreamWriter (Open (FileMode.Create, FileAccess.Write));
  132. }
  133. public StreamWriter AppendText () {
  134. return new StreamWriter (Open (FileMode.Append, FileAccess.Write));
  135. }
  136. // filestream methods
  137. public FileStream Create ()
  138. {
  139. return File.Create (FullPath);
  140. }
  141. public FileStream OpenRead () {
  142. return Open (FileMode.Open, FileAccess.Read, FileShare.Read);
  143. }
  144. public FileStream OpenWrite () {
  145. return Open (FileMode.OpenOrCreate, FileAccess.Write);
  146. }
  147. public FileStream Open (FileMode mode) {
  148. return Open (mode, FileAccess.ReadWrite);
  149. }
  150. public FileStream Open (FileMode mode, FileAccess access) {
  151. return Open (mode, access, FileShare.None);
  152. }
  153. public FileStream Open (FileMode mode, FileAccess access, FileShare share) {
  154. return new FileStream (FullPath, mode, access, share);
  155. }
  156. // file methods
  157. public override void Delete () {
  158. MonoIOError error;
  159. if (!MonoIO.Exists (FullPath, out error)) {
  160. // a weird MS.NET behaviour
  161. return;
  162. }
  163. if (MonoIO.ExistsDirectory (FullPath, out error)) {
  164. throw new UnauthorizedAccessException ("Access to the path \"" + FullPath + "\" is denied.");
  165. }
  166. if (!MonoIO.DeleteFile (FullPath, out error)) {
  167. throw MonoIO.GetException (OriginalPath,
  168. error);
  169. }
  170. }
  171. public void MoveTo (string dest) {
  172. if (dest == null)
  173. throw new ArgumentNullException ();
  174. if (dest == Name || dest == FullName)
  175. return;
  176. MonoIOError error;
  177. if (MonoIO.Exists (dest, out error) ||
  178. MonoIO.ExistsDirectory (dest, out error))
  179. throw new IOException ();
  180. File.Move (FullPath, dest);
  181. this.FullPath = Path.GetFullPath (dest);
  182. }
  183. public FileInfo CopyTo (string path) {
  184. return CopyTo (path, false);
  185. }
  186. public FileInfo CopyTo (string path, bool overwrite) {
  187. string dest = Path.GetFullPath (path);
  188. if (overwrite && File.Exists (path))
  189. File.Delete (path);
  190. File.Copy (FullPath, dest);
  191. return new FileInfo (dest);
  192. }
  193. public override string ToString () {
  194. return OriginalPath;
  195. }
  196. #if NET_2_0
  197. public FileSecurity GetAccessControl ()
  198. {
  199. throw new NotImplementedException ();
  200. }
  201. public FileSecurity GetAccessControl (AccessControlSections includeSections)
  202. {
  203. throw new NotImplementedException ();
  204. }
  205. [ComVisible (false)]
  206. public FileInfo Replace (string destinationFileName,
  207. string destinationBackupFileName)
  208. {
  209. throw new NotImplementedException ();
  210. }
  211. [ComVisible (false)]
  212. public FileInfo Replace (string destinationFileName,
  213. string destinationBackupFileName,
  214. bool ignoreMetadataErrors)
  215. {
  216. throw new NotImplementedException ();
  217. }
  218. public void SetAccessControl (FileSecurity fileSecurity)
  219. {
  220. throw new NotImplementedException ();
  221. }
  222. #endif
  223. }
  224. }