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 = false;
23 public FileInfo (string path) {
\r
26 OriginalPath = path;
\r
27 FullPath = Path.GetFullPath (path);
28 exists = File.Exists (path);
31 // public properties
\r
33 public override bool Exists {
\r
37 if (stat.Attributes == MonoIO.InvalidFileAttributes)
\r
40 if ((stat.Attributes & FileAttributes.Directory) != 0)
\r
47 public override string Name {
\r
49 return Path.GetFileName (FullPath);
\r
53 public long Length {
\r
56 throw new FileNotFoundException ("Could not find file \"" + OriginalPath + "\".");
\r
62 public string DirectoryName {
\r
64 return Path.GetDirectoryName (FullPath);
\r
68 public DirectoryInfo Directory {
\r
70 return new DirectoryInfo (DirectoryName);
\r
74 // streamreader methods
\r
76 public StreamReader OpenText () {
\r
77 return new StreamReader (Open (FileMode.Open, FileAccess.Read));
\r
80 public StreamWriter CreateText () {
\r
81 return new StreamWriter (Open (FileMode.Create, FileAccess.Write));
\r
84 public StreamWriter AppendText () {
\r
85 return new StreamWriter (Open (FileMode.Append, FileAccess.Write));
\r
88 // filestream methods
\r
90 public FileStream Create ()
92 return File.Create (FullPath);
96 public FileStream OpenRead () {
\r
97 return Open (FileMode.Open, FileAccess.Read);
\r
100 public FileStream OpenWrite () {
\r
101 return Open (FileMode.OpenOrCreate, FileAccess.Write);
\r
104 public FileStream Open (FileMode mode) {
\r
105 return Open (mode, FileAccess.ReadWrite);
\r
108 public FileStream Open (FileMode mode, FileAccess access) {
\r
109 return Open (mode, access, FileShare.None);
\r
112 public FileStream Open (FileMode mode, FileAccess access, FileShare share) {
\r
113 return new FileStream (FullPath, mode, access, share);
\r
118 public override void Delete () {
121 if (!MonoIO.Exists (FullPath, out error)) {
122 // a weird MS.NET behaviour
126 if (MonoIO.ExistsDirectory (FullPath, out error)) {
127 throw new UnauthorizedAccessException ("Access to the path \"" + FullPath + "\" is denied.");
130 if (!MonoIO.DeleteFile (FullPath, out error)) {
131 throw MonoIO.GetException (OriginalPath,
136 public void MoveTo (string dest) {
138 throw new ArgumentNullException ();
140 if (MonoIO.Exists (dest, out error) ||
141 MonoIO.ExistsDirectory (dest, out error))
142 throw new IOException ();
143 File.Move (FullPath, dest);
146 public FileInfo CopyTo (string path) {
\r
147 return CopyTo (path, false);
\r
150 public FileInfo CopyTo (string path, bool overwrite) {
\r
151 string dest = Path.GetFullPath (path);
\r
153 if (overwrite && File.Exists (path))
156 File.Copy (FullPath, dest);
\r
158 return new FileInfo (dest);
\r
161 public override string ToString () {
\r
162 return OriginalPath;
\r