mscorlib(4.0.0.0) API with additions
FileSystemInfo.cs
1 using Microsoft.Win32;
4 using System.Security;
6 
7 namespace System.IO
8 {
10  [Serializable]
11  [ComVisible(true)]
12  [FileIOPermission(SecurityAction.InheritanceDemand, Unrestricted = true)]
14  {
15  [SecurityCritical]
16  internal Win32Native.WIN32_FILE_ATTRIBUTE_DATA _data;
17 
18  internal int _dataInitialised = -1;
19 
20  private const int ERROR_INVALID_PARAMETER = 87;
21 
22  internal const int ERROR_ACCESS_DENIED = 5;
23 
26  protected string FullPath;
27 
29  protected string OriginalPath;
30 
31  private string _displayPath = "";
32 
37  public virtual string FullName
38  {
39  [SecuritySafeCritical]
40  get
41  {
42  FileIOPermission.QuickDemand(FileIOPermissionAccess.PathDiscovery, FullPath);
43  return FullPath;
44  }
45  }
46 
47  internal virtual string UnsafeGetFullName
48  {
49  [SecurityCritical]
50  get
51  {
52  FileIOPermission.QuickDemand(FileIOPermissionAccess.PathDiscovery, FullPath);
53  return FullPath;
54  }
55  }
56 
59  public string Extension
60  {
61  get
62  {
63  int length = FullPath.Length;
64  int num = length;
65  while (--num >= 0)
66  {
67  char c = FullPath[num];
68  if (c == '.')
69  {
70  return FullPath.Substring(num, length - num);
71  }
73  {
74  break;
75  }
76  }
77  return string.Empty;
78  }
79  }
80 
83  public abstract string Name
84  {
85  get;
86  }
87 
91  public abstract bool Exists
92  {
93  get;
94  }
95 
103  public DateTime CreationTime
104  {
105  get
106  {
107  return CreationTimeUtc.ToLocalTime();
108  }
109  set
110  {
111  CreationTimeUtc = value.ToUniversalTime();
112  }
113  }
114 
122  [ComVisible(false)]
124  {
125  [SecuritySafeCritical]
126  get
127  {
128  if (_dataInitialised == -1)
129  {
130  _data = default(Win32Native.WIN32_FILE_ATTRIBUTE_DATA);
131  Refresh();
132  }
133  if (_dataInitialised != 0)
134  {
135  __Error.WinIOError(_dataInitialised, DisplayPath);
136  }
137  long fileTime = (long)(((ulong)_data.ftCreationTimeHigh << 32) | _data.ftCreationTimeLow);
138  return DateTime.FromFileTimeUtc(fileTime);
139  }
140  set
141  {
142  if (this is DirectoryInfo)
143  {
145  }
146  else
147  {
149  }
150  _dataInitialised = -1;
151  }
152  }
153 
160  public DateTime LastAccessTime
161  {
162  get
163  {
165  }
166  set
167  {
168  LastAccessTimeUtc = value.ToUniversalTime();
169  }
170  }
171 
178  [ComVisible(false)]
180  {
181  [SecuritySafeCritical]
182  get
183  {
184  if (_dataInitialised == -1)
185  {
186  _data = default(Win32Native.WIN32_FILE_ATTRIBUTE_DATA);
187  Refresh();
188  }
189  if (_dataInitialised != 0)
190  {
191  __Error.WinIOError(_dataInitialised, DisplayPath);
192  }
193  long fileTime = (long)(((ulong)_data.ftLastAccessTimeHigh << 32) | _data.ftLastAccessTimeLow);
194  return DateTime.FromFileTimeUtc(fileTime);
195  }
196  set
197  {
198  if (this is DirectoryInfo)
199  {
201  }
202  else
203  {
205  }
206  _dataInitialised = -1;
207  }
208  }
209 
216  public DateTime LastWriteTime
217  {
218  get
219  {
220  return LastWriteTimeUtc.ToLocalTime();
221  }
222  set
223  {
224  LastWriteTimeUtc = value.ToUniversalTime();
225  }
226  }
227 
234  [ComVisible(false)]
236  {
237  [SecuritySafeCritical]
238  get
239  {
240  if (_dataInitialised == -1)
241  {
242  _data = default(Win32Native.WIN32_FILE_ATTRIBUTE_DATA);
243  Refresh();
244  }
245  if (_dataInitialised != 0)
246  {
247  __Error.WinIOError(_dataInitialised, DisplayPath);
248  }
249  long fileTime = (long)(((ulong)_data.ftLastWriteTimeHigh << 32) | _data.ftLastWriteTimeLow);
250  return DateTime.FromFileTimeUtc(fileTime);
251  }
252  set
253  {
254  if (this is DirectoryInfo)
255  {
257  }
258  else
259  {
261  }
262  _dataInitialised = -1;
263  }
264  }
265 
276  {
277  [SecuritySafeCritical]
278  get
279  {
280  if (_dataInitialised == -1)
281  {
282  _data = default(Win32Native.WIN32_FILE_ATTRIBUTE_DATA);
283  Refresh();
284  }
285  if (_dataInitialised != 0)
286  {
287  __Error.WinIOError(_dataInitialised, DisplayPath);
288  }
289  return (FileAttributes)_data.fileAttributes;
290  }
291  [SecuritySafeCritical]
292  set
293  {
295  if (!Win32Native.SetFileAttributes(FullPath, (int)value))
296  {
297  int lastWin32Error = Marshal.GetLastWin32Error();
298  switch (lastWin32Error)
299  {
300  case 87:
301  throw new ArgumentException(Environment.GetResourceString("Arg_InvalidFileAttrs"));
302  case 5:
303  throw new ArgumentException(Environment.GetResourceString("UnauthorizedAccess_IODenied_NoPathName"));
304  }
305  __Error.WinIOError(lastWin32Error, DisplayPath);
306  }
307  _dataInitialised = -1;
308  }
309  }
310 
311  internal string DisplayPath
312  {
313  get
314  {
315  return _displayPath;
316  }
317  set
318  {
319  _displayPath = value;
320  }
321  }
322 
324  protected FileSystemInfo()
325  {
326  }
327 
333  {
334  if (info == null)
335  {
336  throw new ArgumentNullException("info");
337  }
338  FullPath = Path.GetFullPathInternal(info.GetString("FullPath"));
339  OriginalPath = info.GetString("OriginalPath");
340  _dataInitialised = -1;
341  }
342 
343  [SecurityCritical]
344  internal void InitializeFrom(Win32Native.WIN32_FIND_DATA findData)
345  {
346  _data = default(Win32Native.WIN32_FILE_ATTRIBUTE_DATA);
347  _data.PopulateFrom(findData);
348  _dataInitialised = 0;
349  }
350 
354  public abstract void Delete();
355 
358  [SecuritySafeCritical]
359  public void Refresh()
360  {
361  _dataInitialised = File.FillAttributeInfo(FullPath, ref _data, tryagain: false, returnErrorOnNotFound: false);
362  }
363 
367  [SecurityCritical]
368  [ComVisible(false)]
369  public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
370  {
371  FileIOPermission.QuickDemand(FileIOPermissionAccess.PathDiscovery, FullPath);
372  info.AddValue("OriginalPath", OriginalPath, typeof(string));
373  info.AddValue("FullPath", FullPath, typeof(string));
374  }
375  }
376 }
abstract string Name
For files, gets the name of the file. For directories, gets the name of the last directory in the hie...
Exposes instance methods for creating, moving, and enumerating through directories and subdirectories...
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
FileIOPermissionAccess
Specifies the type of file access requested.
abstract bool Exists
Gets a value indicating whether the file or directory exists.
DateTime CreationTime
Gets or sets the creation time of the current file or directory.
static unsafe void SetLastWriteTimeUtc(string path, DateTime lastWriteTimeUtc)
Sets the date and time, in coordinated universal time (UTC), that the specified file was last written...
Definition: File.cs:806
FileAttributes
Provides attributes for files and directories.
Definition: __Canon.cs:3
static readonly char AltDirectorySeparatorChar
Provides a platform-specific alternate character used to separate directory levels in a path string t...
Definition: Path.cs:23
Represents an instant in time, typically expressed as a date and time of day. To browse the ....
Definition: DateTime.cs:13
static readonly char DirectorySeparatorChar
Provides a platform-specific character used to separate directory levels in a path string that reflec...
Definition: Path.cs:17
Describes the source and destination of a given serialized stream, and provides an additional caller-...
static unsafe void SetLastAccessTimeUtc(string path, DateTime lastAccessTimeUtc)
Sets the date and time, in Coordinated Universal Time (UTC) format, that the specified file or direct...
Definition: Directory.cs:550
DateTime LastAccessTimeUtc
Gets or sets the time, in coordinated universal time (UTC), that the current file or directory was la...
static DateTime FromFileTimeUtc(long fileTime)
Converts the specified Windows file time to an equivalent UTC time.
Definition: DateTime.cs:1103
DateTime ToLocalTime()
Converts the value of the current T:System.DateTime object to local time.
Definition: DateTime.cs:1460
FileSystemInfo()
Initializes a new instance of the T:System.IO.FileSystemInfo class.
DateTime LastAccessTime
Gets or sets the time the current file or directory was last accessed.
FileAttributes Attributes
Gets or sets the attributes for the current file or directory.
SecurityAction
Specifies the security actions that can be performed using declarative security.
DateTime CreationTimeUtc
Gets or sets the creation time, in coordinated universal time (UTC), of the current file or directory...
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
static unsafe void SetCreationTimeUtc(string path, DateTime creationTimeUtc)
Sets the date and time, in coordinated universal time (UTC), that the file was created.
Definition: File.cs:612
Exposes static methods for creating, moving, and enumerating through directories and subdirectories....
Definition: Directory.cs:14
static unsafe void SetLastWriteTimeUtc(string path, DateTime lastWriteTimeUtc)
Sets the date and time, in Coordinated Universal Time (UTC) format, that a directory was last written...
Definition: Directory.cs:477
static unsafe void SetCreationTimeUtc(string path, DateTime creationTimeUtc)
Sets the creation date and time, in Coordinated Universal Time (UTC) format, for the specified file o...
Definition: Directory.cs:404
Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks,...
Definition: Marshal.cs:15
Stores all the data needed to serialize or deserialize an object. This class cannot be inherited.
DateTime LastWriteTime
Gets or sets the time when the current file or directory was last written to.
The exception that is thrown when one of the arguments provided to a method is not valid.
FileSystemInfo(SerializationInfo info, StreamingContext context)
Initializes a new instance of the T:System.IO.FileSystemInfo class with serialized data.
Allows an object to control its own serialization and deserialization.
Definition: ISerializable.cs:8
void Refresh()
Refreshes the state of the object.
Specifies that the class can be serialized.
abstract void Delete()
Deletes a file or directory.
string Extension
Gets the string representing the extension part of the file.
DateTime LastWriteTimeUtc
Gets or sets the time, in coordinated universal time (UTC), when the current file or directory was la...
Provides static methods for the creation, copying, deletion, moving, and opening of a single file,...
Definition: File.cs:14
virtual string FullName
Gets the full path of the directory or file.
Controls the ability to access files and folders. This class cannot be inherited.
static int GetLastWin32Error()
Returns the error code returned by the last unmanaged function that was called using platform invoke ...
static unsafe void SetLastAccessTimeUtc(string path, DateTime lastAccessTimeUtc)
Sets the date and time, in coordinated universal time (UTC), that the specified file was last accesse...
Definition: File.cs:709
Provides the base class for both T:System.IO.FileInfo and T:System.IO.DirectoryInfo objects.
string FullPath
Represents the fully qualified path of the directory or file.
virtual void GetObjectData(SerializationInfo info, StreamingContext context)
Sets the T:System.Runtime.Serialization.SerializationInfo object with the file name and additional ex...
Performs operations on T:System.String instances that contain file or directory path information....
Definition: Path.cs:13
static readonly char VolumeSeparatorChar
Provides a platform-specific volume separator character.
Definition: Path.cs:27
Enables access to objects across application domain boundaries in applications that support remoting.
string OriginalPath
The path originally specified by the user, whether relative or absolute.