* FileSystemInfo.cs: corrected COM visibility of UTC properties
[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 using System.Runtime.InteropServices;
15 using System.Runtime.Serialization;
16
17 namespace System.IO {
18         
19         [Serializable]
20         public abstract class FileSystemInfo : MarshalByRefObject, ISerializable {
21                 #region Implementation of ISerializable
22
23                 [ComVisible(false)]
24                 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
25                 {
26                         info.AddValue ("OriginalPath", OriginalPath, typeof(string));
27                         info.AddValue ("FullPath", FullPath, typeof(string));
28                 }
29
30                 #endregion Implementation of ISerializable
31
32                 // public properties
33
34                 public abstract bool Exists { get; }
35
36                 public abstract string Name { get; }
37
38                 public virtual string FullName {
39                         get {
40                                 return FullPath;
41                         }
42                 }
43
44                 public string Extension {
45                         get {
46                                 return Path.GetExtension (Name);
47                         }
48                 }
49
50                 public FileAttributes Attributes {
51                         get {
52                                 Refresh (false);
53                                 return stat.Attributes;
54                         }
55
56                         set {
57                                 MonoIOError error;
58                                 
59                                 if (!MonoIO.SetFileAttributes (FullName,
60                                                                value,
61                                                                out error))
62                                         throw MonoIO.GetException (error);
63                                 Refresh (true);
64                         }
65                 }
66
67                 public DateTime CreationTime {
68                         get {
69                                 Refresh (false);
70                                 return DateTime.FromFileTime (stat.CreationTime);
71                         }
72
73                         set {
74                                 long filetime = value.ToFileTime ();
75                         
76                                 MonoIOError error;
77                                 
78                                 if (!MonoIO.SetFileTime (FullName, filetime,
79                                                          -1, -1, out error))
80                                         throw MonoIO.GetException (error);
81                                 Refresh (true);
82                         }
83                 }
84
85                 [ComVisible(false)]
86                 public DateTime CreationTimeUtc {
87                         get {
88                                 return CreationTime.ToUniversalTime ();
89                         }
90
91                         set {
92                                 CreationTime = value.ToLocalTime ();
93                         }
94                 }
95
96                 public DateTime LastAccessTime {
97                         get {
98                                 Refresh (false);
99                                 return DateTime.FromFileTime (stat.LastAccessTime);
100                         }
101
102                         set {
103                                 long filetime = value.ToFileTime ();
104
105                                 MonoIOError error;
106                                 
107                                 if (!MonoIO.SetFileTime (FullName, -1,
108                                                          filetime, -1,
109                                                          out error))
110                                         throw MonoIO.GetException (error);
111                                 Refresh (true);
112                         }
113                 }
114
115                 [ComVisible(false)]
116                 public DateTime LastAccessTimeUtc {
117                         get {
118                                 Refresh (false);
119                                 return LastAccessTime.ToUniversalTime ();
120                         }
121
122                         set {
123                                 LastAccessTime = value.ToLocalTime ();
124                         }
125                 }
126
127                 public DateTime LastWriteTime {
128                         get {
129                                 Refresh (false);
130                                 return DateTime.FromFileTime (stat.LastWriteTime);
131                         }
132
133                         set {
134                                 long filetime = value.ToFileTime ();
135
136                                 MonoIOError error;
137                                 
138                                 if (!MonoIO.SetFileTime (FullName, -1, -1,
139                                                          filetime, out error))
140                                         throw MonoIO.GetException (error);
141                                 Refresh (true);
142                         }
143                 }
144
145                 [ComVisible(false)]
146                 public DateTime LastWriteTimeUtc {
147                         get {
148                                 Refresh (false);
149                                 return LastWriteTime.ToUniversalTime ();
150                         }
151
152                         set {
153                                 LastWriteTime = value.ToLocalTime ();
154                         }
155                 }
156
157                 // public methods
158
159                 public abstract void Delete ();
160
161                 public void Refresh ()
162                 {
163                         Refresh (true);
164                 }
165
166                 // protected
167
168                 protected FileSystemInfo ()
169                 {
170                         this.valid = false;
171                         this.FullPath = null;
172                 }
173
174                 protected FileSystemInfo (SerializationInfo info, StreamingContext context)
175                 {
176                         if (info == null)
177                         {
178                                 throw new ArgumentNullException("info");
179                         }
180
181                         FullPath = info.GetString("FullPath");
182                         OriginalPath = info.GetString("OriginalPath");
183                 }
184
185                 protected string FullPath;
186                 protected string OriginalPath;
187
188                 // internal
189
190                 internal void Refresh (bool force)
191                 {
192                         if (valid && !force)
193                                 return;
194
195                         MonoIOError error;
196                         
197                         MonoIO.GetFileStat (FullName, out stat, out error);
198                         valid = true;
199                         
200                         InternalRefresh ();
201                 }
202                 
203                 internal virtual void InternalRefresh ()
204                 {
205                 }
206
207                 internal void CheckPath (string path)
208                 {
209                         if (path == null)
210                                 throw new ArgumentNullException ();
211                         if (path.IndexOfAny (Path.InvalidPathChars) != -1)
212                                 throw new ArgumentException ("Invalid characters in path.");
213                 }
214
215                 internal MonoIOStat stat;
216                 internal bool valid;
217         }
218 }