FileInfo.cs 9.9 KB

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