1 //------------------------------------------------------------------------------
3 // System.IO.FileSystemInfo.cs
5 // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
7 // Author: Jim Richardson, develop@wtfo-guru.com
8 // Dan Lewis (dihlewis@yahoo.co.uk)
9 // Created: Monday, August 13, 2001
11 //------------------------------------------------------------------------------
14 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 using System.Runtime.InteropServices;
37 using System.Runtime.Serialization;
38 using System.Security;
39 using System.Security.Permissions;
44 [FileIOPermission (SecurityAction.InheritanceDemand, Unrestricted = true)]
47 public abstract class FileSystemInfo {
49 public abstract class FileSystemInfo : MarshalByRefObject, ISerializable {
51 #region Implementation of ISerializable
54 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
56 info.AddValue ("OriginalPath", OriginalPath, typeof(string));
57 info.AddValue ("FullPath", FullPath, typeof(string));
60 #endregion Implementation of ISerializable
64 public abstract bool Exists { get; }
66 public abstract string Name { get; }
68 public virtual string FullName {
74 public string Extension {
76 return Path.GetExtension (Name);
80 public FileAttributes Attributes {
83 return stat.Attributes;
89 if (!MonoIO.SetFileAttributes (FullName,
92 throw MonoIO.GetException (FullName,
98 public DateTime CreationTime {
101 return DateTime.FromFileTime (stat.CreationTime);
105 SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
107 long filetime = value.ToFileTime ();
111 if (!MonoIO.SetFileTime (FullName, filetime,
113 throw MonoIO.GetException (FullName,
120 public DateTime CreationTimeUtc {
122 return CreationTime.ToUniversalTime ();
126 CreationTime = value.ToLocalTime ();
130 public DateTime LastAccessTime {
133 return DateTime.FromFileTime (stat.LastAccessTime);
137 SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
139 long filetime = value.ToFileTime ();
143 if (!MonoIO.SetFileTime (FullName, -1,
146 throw MonoIO.GetException (FullName,
153 public DateTime LastAccessTimeUtc {
156 return LastAccessTime.ToUniversalTime ();
160 LastAccessTime = value.ToLocalTime ();
164 public DateTime LastWriteTime {
167 return DateTime.FromFileTime (stat.LastWriteTime);
171 SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
173 long filetime = value.ToFileTime ();
177 if (!MonoIO.SetFileTime (FullName, -1, -1,
178 filetime, out error))
179 throw MonoIO.GetException (FullName,
186 public DateTime LastWriteTimeUtc {
189 return LastWriteTime.ToUniversalTime ();
193 LastWriteTime = value.ToLocalTime ();
199 public abstract void Delete ();
201 public void Refresh ()
208 protected FileSystemInfo ()
211 this.FullPath = null;
214 protected FileSystemInfo (SerializationInfo info, StreamingContext context)
218 throw new ArgumentNullException("info");
221 FullPath = info.GetString("FullPath");
222 OriginalPath = info.GetString("OriginalPath");
225 protected string FullPath;
226 protected string OriginalPath;
230 internal void Refresh (bool force)
237 MonoIO.GetFileStat (FullName, out stat, out error);
238 /* Don't throw on error here, too much other
239 * stuff relies on it not doing so...
247 internal virtual void InternalRefresh ()
251 internal void CheckPath (string path)
254 throw new ArgumentNullException ("path");
255 if (path.Length == 0)
256 throw new ArgumentException ("An empty file name is not valid.");
257 if (path.IndexOfAny (Path.InvalidPathChars) != -1)
258 throw new ArgumentException ("Illegal characters in path.");
259 if (Environment.IsRunningOnWindows) {
260 int idx = path.IndexOf (':');
261 if (idx >= 0 && idx != 1)
262 throw new ArgumentException ("path");
266 internal MonoIOStat stat;