* FileSystemInfo.cs: Added InternalRefresh, a virtual method that derived
[mono.git] / mcs / class / corlib / System.IO / FileSystemInfo.cs
1 //------------------------------------------------------------------------------
2 // 
3 // System.IO.FileSystemInfo.cs 
4 //
5 // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
6 // 
7 // Author:         Jim Richardson, develop@wtfo-guru.com
8 //                 Dan Lewis (dihlewis@yahoo.co.uk)
9 // Created:        Monday, August 13, 2001 
10 //
11 //------------------------------------------------------------------------------
12
13 using System;
14
15 namespace System.IO {
16         
17         [Serializable]
18         public abstract class FileSystemInfo : MarshalByRefObject {
19                 // public properties
20
21                 public abstract bool Exists { get; }
22
23                 public abstract string Name { get; }
24
25                 public virtual string FullName {
26                         get {
27                                 return FullPath;
28                         }
29                 }
30
31                 public string Extension {
32                         get {
33                                 return Path.GetExtension (Name);
34                         }
35                 }
36
37                 public FileAttributes Attributes {
38                         get {
39                                 Refresh (false);
40                                 return stat.Attributes;
41                         }
42
43                         set {
44                                 MonoIOError error;
45                                 
46                                 if (!MonoIO.SetFileAttributes (FullName,
47                                                                value,
48                                                                out error))
49                                         throw MonoIO.GetException (error);
50                                 Refresh (true);
51                         }
52                 }
53
54                 public DateTime CreationTime {
55                         get {
56                                 Refresh (false);
57                                 return DateTime.FromFileTime (stat.CreationTime);
58                         }
59
60                         set {
61                                 long filetime = value.ToFileTime ();
62                         
63                                 MonoIOError error;
64                                 
65                                 if (!MonoIO.SetFileTime (FullName, filetime,
66                                                          -1, -1, out error))
67                                         throw MonoIO.GetException (error);
68                                 Refresh (true);
69                         }
70                 }
71
72                 public DateTime CreationTimeUtc {
73                         get {
74                                 return CreationTime.ToUniversalTime ();
75                         }
76
77                         set {
78                                 CreationTime = value.ToLocalTime ();
79                         }
80                 }
81
82                 public DateTime LastAccessTime {
83                         get {
84                                 Refresh (false);
85                                 return DateTime.FromFileTime (stat.LastAccessTime);
86                         }
87
88                         set {
89                                 long filetime = value.ToFileTime ();
90
91                                 MonoIOError error;
92                                 
93                                 if (!MonoIO.SetFileTime (FullName, -1,
94                                                          filetime, -1,
95                                                          out error))
96                                         throw MonoIO.GetException (error);
97                                 Refresh (true);
98                         }
99                 }
100
101                 public DateTime LastAccessTimeUtc {
102                         get {
103                                 Refresh (false);
104                                 return LastAccessTime.ToUniversalTime ();
105                         }
106
107                         set {
108                                 LastAccessTime = value.ToLocalTime ();
109                         }
110                 }
111
112                 public DateTime LastWriteTime {
113                         get {
114                                 Refresh (false);
115                                 return DateTime.FromFileTime (stat.LastWriteTime);
116                         }
117
118                         set {
119                                 long filetime = value.ToFileTime ();
120
121                                 MonoIOError error;
122                                 
123                                 if (!MonoIO.SetFileTime (FullName, -1, -1,
124                                                          filetime, out error))
125                                         throw MonoIO.GetException (error);
126                                 Refresh (true);
127                         }
128                 }
129
130                 public DateTime LastWriteTimeUtc {
131                         get {
132                                 Refresh (false);
133                                 return LastWriteTime.ToUniversalTime ();
134                         }
135
136                         set {
137                                 LastWriteTime = value.ToLocalTime ();
138                         }
139                 }
140
141                 // public methods
142
143                 public abstract void Delete ();
144
145                 public void Refresh ()
146                 {
147                         Refresh (true);
148                 }
149
150                 // protected
151
152                 protected FileSystemInfo ()
153                 {
154                         this.valid = false;
155                         this.FullPath = null;
156                 }
157
158                 protected string FullPath;
159                 protected string OriginalPath;
160
161                 // internal
162
163                 internal void Refresh (bool force)
164                 {
165                         if (valid && !force)
166                                 return;
167
168                         MonoIOError error;
169                         
170                         MonoIO.GetFileStat (FullName, out stat, out error);
171                         valid = true;
172                         
173                         InternalRefresh ();
174                 }
175                 
176                 internal virtual void InternalRefresh ()
177                 {
178                 }
179
180                 internal void CheckPath (string path)
181                 {
182                         if (path == null)
183                                 throw new ArgumentNullException ();
184                         if (path.IndexOfAny (Path.InvalidPathChars) != -1)
185                                 throw new ArgumentException ("Invalid characters in path.");
186                 }
187
188                 internal MonoIOStat stat;
189                 internal bool valid;
190         }
191 }