FileInfo.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. using System.Runtime.Serialization;
  36. #if !NET_2_1
  37. using System.Security.AccessControl;
  38. #endif
  39. namespace System.IO {
  40. [Serializable]
  41. [ComVisible (true)]
  42. public sealed class FileInfo : FileSystemInfo
  43. {
  44. private bool exists;
  45. public FileInfo (string fileName)
  46. {
  47. if (fileName == null)
  48. throw new ArgumentNullException ("fileName");
  49. CheckPath (fileName);
  50. OriginalPath = fileName;
  51. FullPath = Path.GetFullPath (fileName);
  52. }
  53. private FileInfo (SerializationInfo info, StreamingContext context)
  54. : base (info, context)
  55. {
  56. }
  57. internal override void InternalRefresh ()
  58. {
  59. exists = File.Exists (FullPath);
  60. }
  61. // public properties
  62. public override bool Exists {
  63. get {
  64. Refresh (false);
  65. if (stat.Attributes == MonoIO.InvalidFileAttributes)
  66. return false;
  67. if ((stat.Attributes & FileAttributes.Directory) != 0)
  68. return false;
  69. return exists;
  70. }
  71. }
  72. public override string Name {
  73. get {
  74. return Path.GetFileName (FullPath);
  75. }
  76. }
  77. #if !NET_2_1
  78. public bool IsReadOnly {
  79. get {
  80. if (!Exists)
  81. throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
  82. return ((stat.Attributes & FileAttributes.ReadOnly) != 0);
  83. }
  84. set {
  85. if (!Exists)
  86. throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
  87. FileAttributes attrs = File.GetAttributes(FullPath);
  88. if (value)
  89. attrs |= FileAttributes.ReadOnly;
  90. else
  91. attrs &= ~FileAttributes.ReadOnly;
  92. File.SetAttributes(FullPath, attrs);
  93. }
  94. }
  95. [MonoLimitation ("File encryption isn't supported (even on NTFS).")]
  96. [ComVisible (false)]
  97. public void Encrypt ()
  98. {
  99. // MS.NET support this only on NTFS file systems, i.e. it's a file-system (not a framework) feature.
  100. // otherwise it throws a NotSupportedException (or a PlatformNotSupportedException on older OS).
  101. // we throw the same (instead of a NotImplementedException) because most code should already be
  102. // handling this exception to work properly.
  103. throw new NotSupportedException (Locale.GetText ("File encryption isn't supported on any file system."));
  104. }
  105. [MonoLimitation ("File encryption isn't supported (even on NTFS).")]
  106. [ComVisible (false)]
  107. public void Decrypt ()
  108. {
  109. // MS.NET support this only on NTFS file systems, i.e. it's a file-system (not a framework) feature.
  110. // otherwise it throws a NotSupportedException (or a PlatformNotSupportedException on older OS).
  111. // we throw the same (instead of a NotImplementedException) because most code should already be
  112. // handling this exception to work properly.
  113. throw new NotSupportedException (Locale.GetText ("File encryption isn't supported on any file system."));
  114. }
  115. #endif
  116. public long Length {
  117. get {
  118. if (!Exists)
  119. throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".", OriginalPath);
  120. return stat.Length;
  121. }
  122. }
  123. public string DirectoryName {
  124. get {
  125. return Path.GetDirectoryName (FullPath);
  126. }
  127. }
  128. public DirectoryInfo Directory {
  129. get {
  130. return new DirectoryInfo (DirectoryName);
  131. }
  132. }
  133. // streamreader methods
  134. public StreamReader OpenText ()
  135. {
  136. return new StreamReader (Open (FileMode.Open, FileAccess.Read));
  137. }
  138. public StreamWriter CreateText ()
  139. {
  140. return new StreamWriter (Open (FileMode.Create, FileAccess.Write));
  141. }
  142. public StreamWriter AppendText ()
  143. {
  144. return new StreamWriter (Open (FileMode.Append, FileAccess.Write));
  145. }
  146. // filestream methods
  147. public FileStream Create ()
  148. {
  149. return File.Create (FullPath);
  150. }
  151. public FileStream OpenRead ()
  152. {
  153. return Open (FileMode.Open, FileAccess.Read, FileShare.Read);
  154. }
  155. public FileStream OpenWrite ()
  156. {
  157. return Open (FileMode.OpenOrCreate, FileAccess.Write);
  158. }
  159. public FileStream Open (FileMode mode)
  160. {
  161. return Open (mode, FileAccess.ReadWrite);
  162. }
  163. public FileStream Open (FileMode mode, FileAccess access)
  164. {
  165. return Open (mode, access, FileShare.None);
  166. }
  167. public FileStream Open (FileMode mode, FileAccess access, FileShare share)
  168. {
  169. return new FileStream (FullPath, mode, access, share);
  170. }
  171. // file methods
  172. public override void Delete ()
  173. {
  174. MonoIOError error;
  175. if (!MonoIO.Exists (FullPath, out error))
  176. // a weird MS.NET behaviour
  177. return;
  178. if (MonoIO.ExistsDirectory (FullPath, out error))
  179. throw new UnauthorizedAccessException ("Access to the path \"" + FullPath + "\" is denied.");
  180. if (!MonoIO.DeleteFile (FullPath, out error))
  181. throw MonoIO.GetException (OriginalPath, error);
  182. }
  183. public void MoveTo (string destFileName)
  184. {
  185. if (destFileName == null)
  186. throw new ArgumentNullException ("destFileName");
  187. if (destFileName == Name || destFileName == FullName)
  188. return;
  189. if (!File.Exists (FullPath))
  190. throw new FileNotFoundException ();
  191. File.Move (FullPath, destFileName);
  192. this.FullPath = Path.GetFullPath (destFileName);
  193. }
  194. public FileInfo CopyTo (string destFileName)
  195. {
  196. return CopyTo (destFileName, false);
  197. }
  198. public FileInfo CopyTo (string destFileName, bool overwrite)
  199. {
  200. if (destFileName == null)
  201. throw new ArgumentNullException ("destFileName");
  202. if (destFileName.Length == 0)
  203. throw new ArgumentException ("An empty file name is not valid.", "destFileName");
  204. string dest = Path.GetFullPath (destFileName);
  205. if (overwrite && File.Exists (dest))
  206. File.Delete (dest);
  207. File.Copy (FullPath, dest);
  208. return new FileInfo (dest);
  209. }
  210. public override string ToString ()
  211. {
  212. #if NET_2_1
  213. // for Moonlight we *never* return paths, since ToString is not [SecurityCritical] we simply return the Name
  214. return Name;
  215. #else
  216. return OriginalPath;
  217. #endif
  218. }
  219. #if !NET_2_1
  220. public FileSecurity GetAccessControl ()
  221. {
  222. throw new NotImplementedException ();
  223. }
  224. public FileSecurity GetAccessControl (AccessControlSections includeSections)
  225. {
  226. throw new NotImplementedException ();
  227. }
  228. [ComVisible (false)]
  229. public FileInfo Replace (string destinationFileName,
  230. string destinationBackupFileName)
  231. {
  232. string destinationFullPath = null;
  233. if (!Exists)
  234. throw new FileNotFoundException ();
  235. if (destinationFileName == null)
  236. throw new ArgumentNullException ("destinationFileName");
  237. if (destinationFileName.Length == 0)
  238. throw new ArgumentException ("An empty file name is not valid.", "destinationFileName");
  239. destinationFullPath = Path.GetFullPath (destinationFileName);
  240. if (!File.Exists (destinationFullPath))
  241. throw new FileNotFoundException ();
  242. FileAttributes attrs = File.GetAttributes (destinationFullPath);
  243. if ( (attrs & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
  244. throw new UnauthorizedAccessException ();
  245. if (destinationBackupFileName != null) {
  246. if (destinationBackupFileName.Length == 0)
  247. throw new ArgumentException ("An empty file name is not valid.", "destinationBackupFileName");
  248. File.Copy (destinationFullPath, Path.GetFullPath (destinationBackupFileName), true);
  249. }
  250. File.Copy (FullPath, destinationFullPath,true);
  251. File.Delete (FullPath);
  252. return new FileInfo (destinationFullPath);
  253. }
  254. [ComVisible (false)]
  255. [MonoLimitation ("We ignore the ignoreMetadataErrors parameter")]
  256. public FileInfo Replace (string destinationFileName,
  257. string destinationBackupFileName,
  258. bool ignoreMetadataErrors)
  259. {
  260. return Replace (destinationFileName, destinationBackupFileName);
  261. }
  262. public void SetAccessControl (FileSecurity fileSecurity)
  263. {
  264. throw new NotImplementedException ();
  265. }
  266. #endif
  267. }
  268. }