1 //------------------------------------------------------------------------------
3 // System.IO.FileSystemInfo.cs
5 // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
6 // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
8 // Author: Jim Richardson, develop@wtfo-guru.com
9 // Dan Lewis (dihlewis@yahoo.co.uk)
10 // Created: Monday, August 13, 2001
12 //------------------------------------------------------------------------------
15 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
17 // Permission is hereby granted, free of charge, to any person obtaining
18 // a copy of this software and associated documentation files (the
19 // "Software"), to deal in the Software without restriction, including
20 // without limitation the rights to use, copy, modify, merge, publish,
21 // distribute, sublicense, and/or sell copies of the Software, and to
22 // permit persons to whom the Software is furnished to do so, subject to
23 // the following conditions:
25 // The above copyright notice and this permission notice shall be
26 // included in all copies or substantial portions of the Software.
28 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 using System.Runtime.InteropServices;
38 using System.Runtime.Serialization;
39 using System.Security;
40 using System.Security.Permissions;
45 [FileIOPermission (SecurityAction.InheritanceDemand, Unrestricted = true)]
48 public abstract class FileSystemInfo {
50 public abstract class FileSystemInfo : MarshalByRefObject, ISerializable {
52 #region Implementation of ISerializable
55 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
57 info.AddValue ("OriginalPath", OriginalPath, typeof(string));
58 info.AddValue ("FullPath", FullPath, typeof(string));
61 #endregion Implementation of ISerializable
65 public abstract bool Exists { get; }
67 public abstract string Name { get; }
69 public virtual string FullName {
75 public string Extension {
77 return Path.GetExtension (Name);
81 public FileAttributes Attributes {
84 return stat.Attributes;
90 if (!MonoIO.SetFileAttributes (FullName,
93 throw MonoIO.GetException (FullName,
99 public DateTime CreationTime {
102 return DateTime.FromFileTime (stat.CreationTime);
106 SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
108 long filetime = value.ToFileTime ();
112 if (!MonoIO.SetFileTime (FullName, filetime,
114 throw MonoIO.GetException (FullName,
121 public DateTime CreationTimeUtc {
123 return CreationTime.ToUniversalTime ();
127 CreationTime = value.ToLocalTime ();
131 public DateTime LastAccessTime {
134 return DateTime.FromFileTime (stat.LastAccessTime);
138 SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
140 long filetime = value.ToFileTime ();
144 if (!MonoIO.SetFileTime (FullName, -1,
147 throw MonoIO.GetException (FullName,
154 public DateTime LastAccessTimeUtc {
157 return LastAccessTime.ToUniversalTime ();
161 LastAccessTime = value.ToLocalTime ();
165 public DateTime LastWriteTime {
168 return DateTime.FromFileTime (stat.LastWriteTime);
172 SecurityManager.EnsureElevatedPermissions (); // this is a no-op outside moonlight
174 long filetime = value.ToFileTime ();
178 if (!MonoIO.SetFileTime (FullName, -1, -1,
179 filetime, out error))
180 throw MonoIO.GetException (FullName,
187 public DateTime LastWriteTimeUtc {
190 return LastWriteTime.ToUniversalTime ();
194 LastWriteTime = value.ToLocalTime ();
200 public abstract void Delete ();
202 public void Refresh ()
209 protected FileSystemInfo ()
212 this.FullPath = null;
215 protected FileSystemInfo (SerializationInfo info, StreamingContext context)
219 throw new ArgumentNullException("info");
222 FullPath = info.GetString("FullPath");
223 OriginalPath = info.GetString("OriginalPath");
226 protected string FullPath;
227 protected string OriginalPath;
231 internal void Refresh (bool force)
238 MonoIO.GetFileStat (FullName, out stat, out error);
239 /* Don't throw on error here, too much other
240 * stuff relies on it not doing so...
248 internal virtual void InternalRefresh ()
252 internal void CheckPath (string path)
255 throw new ArgumentNullException ("path");
256 if (path.Length == 0)
257 throw new ArgumentException ("An empty file name is not valid.");
258 if (path.IndexOfAny (Path.InvalidPathChars) != -1)
259 throw new ArgumentException ("Illegal characters in path.");
260 if (Environment.IsRunningOnWindows) {
261 int idx = path.IndexOf (':');
262 if (idx >= 0 && idx != 1)
263 throw new ArgumentException ("path");
267 internal MonoIOStat stat;