1 //------------------------------------------------------------------------------
\r
3 // System.IO.FileInfo.cs
\r
5 // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
\r
7 // Author: Jim Richardson, develop@wtfo-guru.com
\r
8 // Dan Lewis (dihlewis@yahoo.co.uk)
\r
9 // Created: Monday, August 13, 2001
\r
11 //------------------------------------------------------------------------------
\r
15 namespace System.IO {
\r
18 public sealed class FileInfo : FileSystemInfo {
\r
21 private bool exists;
\r
23 public FileInfo (string path) {
\r
26 OriginalPath = path;
\r
27 FullPath = Path.GetFullPath (path);
\r
30 internal override void InternalRefresh ()
\r
32 exists = File.Exists (FullPath);
\r
36 // public properties
\r
38 public override bool Exists {
\r
42 if (stat.Attributes == MonoIO.InvalidFileAttributes)
\r
45 if ((stat.Attributes & FileAttributes.Directory) != 0)
\r
52 public override string Name {
\r
54 return Path.GetFileName (FullPath);
\r
58 public long Length {
\r
61 throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".");
\r
67 public string DirectoryName {
\r
69 return Path.GetDirectoryName (FullPath);
\r
73 public DirectoryInfo Directory {
\r
75 return new DirectoryInfo (DirectoryName);
\r
79 // streamreader methods
\r
81 public StreamReader OpenText () {
\r
82 return new StreamReader (Open (FileMode.Open, FileAccess.Read));
\r
85 public StreamWriter CreateText () {
\r
86 return new StreamWriter (Open (FileMode.Create, FileAccess.Write));
\r
89 public StreamWriter AppendText () {
\r
90 return new StreamWriter (Open (FileMode.Append, FileAccess.Write));
\r
93 // filestream methods
\r
95 public FileStream Create ()
\r
97 return File.Create (FullPath);
\r
101 public FileStream OpenRead () {
\r
102 return Open (FileMode.Open, FileAccess.Read);
\r
105 public FileStream OpenWrite () {
\r
106 return Open (FileMode.OpenOrCreate, FileAccess.Write);
\r
109 public FileStream Open (FileMode mode) {
\r
110 return Open (mode, FileAccess.ReadWrite);
\r
113 public FileStream Open (FileMode mode, FileAccess access) {
\r
114 return Open (mode, access, FileShare.None);
\r
117 public FileStream Open (FileMode mode, FileAccess access, FileShare share) {
\r
118 return new FileStream (FullPath, mode, access, share);
\r
123 public override void Delete () {
\r
126 if (!MonoIO.Exists (FullPath, out error)) {
\r
127 // a weird MS.NET behaviour
\r
131 if (MonoIO.ExistsDirectory (FullPath, out error)) {
\r
132 throw new UnauthorizedAccessException ("Access to the path \"" + FullPath + "\" is denied.");
\r
135 if (!MonoIO.DeleteFile (FullPath, out error)) {
\r
136 throw MonoIO.GetException (OriginalPath,
\r
141 public void MoveTo (string dest) {
\r
144 throw new ArgumentNullException ();
\r
146 if (dest == Name || dir == FullName)
\r
150 if (MonoIO.Exists (dest, out error) ||
\r
151 MonoIO.ExistsDirectory (dest, out error))
\r
152 throw new IOException ();
\r
153 File.Move (FullPath, dest);
\r
154 this.FullPath = Path.GetFullPath (dest);
\r
157 public FileInfo CopyTo (string path) {
\r
158 return CopyTo (path, false);
\r
161 public FileInfo CopyTo (string path, bool overwrite) {
\r
162 string dest = Path.GetFullPath (path);
\r
164 if (overwrite && File.Exists (path))
\r
165 File.Delete (path);
\r
167 File.Copy (FullPath, dest);
\r
169 return new FileInfo (dest);
\r
172 public override string ToString () {
\r