mscorlib(4.0.0.0) API with additions
FileInfo.cs
1 using Microsoft.Win32;
4 using System.Security;
7 using System.Text;
8 
9 namespace System.IO
10 {
12  [Serializable]
13  [ComVisible(true)]
14  public sealed class FileInfo : FileSystemInfo
15  {
16  private string _name;
17 
20  public override string Name => _name;
21 
27  public long Length
28  {
29  [SecuritySafeCritical]
30  get
31  {
32  if (_dataInitialised == -1)
33  {
34  Refresh();
35  }
36  if (_dataInitialised != 0)
37  {
38  __Error.WinIOError(_dataInitialised, base.DisplayPath);
39  }
40  if ((_data.fileAttributes & 0x10) != 0)
41  {
42  __Error.WinIOError(2, base.DisplayPath);
43  }
44  return ((long)_data.fileSizeHigh << 32) | (_data.fileSizeLow & uint.MaxValue);
45  }
46  }
47 
54  public string DirectoryName
55  {
56  [SecuritySafeCritical]
57  get
58  {
59  string directoryName = Path.GetDirectoryName(FullPath);
60  if (directoryName != null)
61  {
62  FileIOPermission.QuickDemand(FileIOPermissionAccess.PathDiscovery, directoryName, checkForDuplicates: false, needFullPath: false);
63  }
64  return directoryName;
65  }
66  }
67 
73  {
74  get
75  {
76  string directoryName = DirectoryName;
77  if (directoryName == null)
78  {
79  return null;
80  }
81  return new DirectoryInfo(directoryName);
82  }
83  }
84 
92  public bool IsReadOnly
93  {
94  get
95  {
96  return (base.Attributes & FileAttributes.ReadOnly) != (FileAttributes)0;
97  }
98  set
99  {
100  if (value)
101  {
102  base.Attributes |= FileAttributes.ReadOnly;
103  }
104  else
105  {
106  base.Attributes &= ~FileAttributes.ReadOnly;
107  }
108  }
109  }
110 
114  public override bool Exists
115  {
116  [SecuritySafeCritical]
117  get
118  {
119  try
120  {
121  if (_dataInitialised == -1)
122  {
123  Refresh();
124  }
125  if (_dataInitialised != 0)
126  {
127  return false;
128  }
129  return (_data.fileAttributes & 0x10) == 0;
130  }
131  catch
132  {
133  return false;
134  }
135  }
136  }
137 
148  [SecuritySafeCritical]
149  public FileInfo(string fileName)
150  {
151  if (fileName == null)
152  {
153  throw new ArgumentNullException("fileName");
154  }
155  Init(fileName, checkHost: true);
156  }
157 
158  [SecurityCritical]
159  private void Init(string fileName, bool checkHost)
160  {
161  OriginalPath = fileName;
162  string fullPathInternal = Path.GetFullPathInternal(fileName);
163  FileIOPermission.QuickDemand(FileIOPermissionAccess.Read, fullPathInternal, checkForDuplicates: false, needFullPath: false);
164  _name = Path.GetFileName(fileName);
165  FullPath = fullPathInternal;
166  base.DisplayPath = GetDisplayPath(fileName);
167  }
168 
169  private string GetDisplayPath(string originalPath)
170  {
171  return originalPath;
172  }
173 
174  [SecurityCritical]
175  private FileInfo(SerializationInfo info, StreamingContext context)
176  : base(info, context)
177  {
178  FileIOPermission.QuickDemand(FileIOPermissionAccess.Read, FullPath, checkForDuplicates: false, needFullPath: false);
179  _name = Path.GetFileName(OriginalPath);
180  base.DisplayPath = GetDisplayPath(OriginalPath);
181  }
182 
183  internal FileInfo(string fullPath, bool ignoreThis)
184  {
185  _name = Path.GetFileName(fullPath);
186  OriginalPath = _name;
187  FullPath = fullPath;
188  base.DisplayPath = _name;
189  }
190 
199  {
201  }
202 
212  {
213  return File.GetAccessControl(FullPath, includeSections);
214  }
215 
222  public void SetAccessControl(FileSecurity fileSecurity)
223  {
224  File.SetAccessControl(FullPath, fileSecurity);
225  }
226 
234  [SecuritySafeCritical]
236  {
237  return new StreamReader(FullPath, Encoding.UTF8, detectEncodingFromByteOrderMarks: true, StreamReader.DefaultBufferSize, checkHost: false);
238  }
239 
246  {
247  return new StreamWriter(FullPath, append: false);
248  }
249 
253  {
254  return new StreamWriter(FullPath, append: true);
255  }
256 
271  public FileInfo CopyTo(string destFileName)
272  {
273  if (destFileName == null)
274  {
275  throw new ArgumentNullException("destFileName", Environment.GetResourceString("ArgumentNull_FileName"));
276  }
277  if (destFileName.Length == 0)
278  {
279  throw new ArgumentException(Environment.GetResourceString("Argument_EmptyFileName"), "destFileName");
280  }
281  destFileName = File.InternalCopy(FullPath, destFileName, overwrite: false, checkHost: true);
282  return new FileInfo(destFileName, ignoreThis: false);
283  }
284 
301  public FileInfo CopyTo(string destFileName, bool overwrite)
302  {
303  if (destFileName == null)
304  {
305  throw new ArgumentNullException("destFileName", Environment.GetResourceString("ArgumentNull_FileName"));
306  }
307  if (destFileName.Length == 0)
308  {
309  throw new ArgumentException(Environment.GetResourceString("Argument_EmptyFileName"), "destFileName");
310  }
311  destFileName = File.InternalCopy(FullPath, destFileName, overwrite, checkHost: true);
312  return new FileInfo(destFileName, ignoreThis: false);
313  }
314 
318  {
319  return File.Create(FullPath);
320  }
321 
326  [SecuritySafeCritical]
327  public override void Delete()
328  {
329  FileIOPermission.QuickDemand(FileIOPermissionAccess.Write, FullPath, checkForDuplicates: false, needFullPath: false);
330  if (!Win32Native.DeleteFile(FullPath))
331  {
332  int lastWin32Error = Marshal.GetLastWin32Error();
333  if (lastWin32Error != 2)
334  {
335  __Error.WinIOError(lastWin32Error, base.DisplayPath);
336  }
337  }
338  }
339 
347  [ComVisible(false)]
348  public void Decrypt()
349  {
351  }
352 
360  [ComVisible(false)]
361  public void Encrypt()
362  {
364  }
365 
373  public FileStream Open(FileMode mode)
374  {
375  return Open(mode, FileAccess.ReadWrite, FileShare.None);
376  }
377 
388  public FileStream Open(FileMode mode, FileAccess access)
389  {
390  return Open(mode, access, FileShare.None);
391  }
392 
404  public FileStream Open(FileMode mode, FileAccess access, FileShare share)
405  {
406  return new FileStream(FullPath, mode, access, share);
407  }
408 
416  {
417  return new FileStream(FullPath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, useAsync: false);
418  }
419 
425  {
426  return new FileStream(FullPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
427  }
428 
444  [SecuritySafeCritical]
445  public void MoveTo(string destFileName)
446  {
447  if (destFileName == null)
448  {
449  throw new ArgumentNullException("destFileName");
450  }
451  if (destFileName.Length == 0)
452  {
453  throw new ArgumentException(Environment.GetResourceString("Argument_EmptyFileName"), "destFileName");
454  }
455  string fullPathInternal = Path.GetFullPathInternal(destFileName);
456  FileIOPermission.QuickDemand(FileIOPermissionAccess.Read | FileIOPermissionAccess.Write, FullPath, checkForDuplicates: false, needFullPath: false);
457  FileIOPermission.QuickDemand(FileIOPermissionAccess.Write, fullPathInternal, checkForDuplicates: false, needFullPath: false);
458  if (!Win32Native.MoveFile(FullPath, fullPathInternal))
459  {
460  __Error.WinIOError();
461  }
462  FullPath = fullPathInternal;
463  OriginalPath = destFileName;
464  _name = Path.GetFileName(fullPathInternal);
465  base.DisplayPath = GetDisplayPath(destFileName);
466  _dataInitialised = -1;
467  }
468 
477  [ComVisible(false)]
478  public FileInfo Replace(string destinationFileName, string destinationBackupFileName)
479  {
480  return Replace(destinationFileName, destinationBackupFileName, ignoreMetadataErrors: false);
481  }
482 
493  [ComVisible(false)]
494  public FileInfo Replace(string destinationFileName, string destinationBackupFileName, bool ignoreMetadataErrors)
495  {
496  File.Replace(FullPath, destinationFileName, destinationBackupFileName, ignoreMetadataErrors);
497  return new FileInfo(destinationFileName);
498  }
499 
502  public override string ToString()
503  {
504  return base.DisplayPath;
505  }
506  }
507 }
Represents a character encoding.To browse the .NET Framework source code for this type,...
Definition: Encoding.cs:15
override string Name
Gets the name of the file.
Definition: FileInfo.cs:20
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.
string DirectoryName
Gets a string representing the directory's full path.
Definition: FileInfo.cs:55
static FileStream Create(string path)
Creates or overwrites a file in the specified path.
Definition: File.cs:231
static void Encrypt(string path)
Encrypts a file so that only the account used to encrypt the file can decrypt it.
Definition: File.cs:406
FileMode
Specifies how the operating system should open a file.
Definition: FileMode.cs:8
FileInfo(string fileName)
Initializes a new instance of the T:System.IO.FileInfo class, which acts as a wrapper for a file path...
Definition: FileInfo.cs:149
Represents the access control and audit security for a file. This class cannot be inherited.
Definition: FileSecurity.cs:8
FileAttributes
Provides attributes for files and directories.
override void Delete()
Permanently deletes a file.
Definition: FileInfo.cs:327
Definition: __Canon.cs:3
FileStream Open(FileMode mode, FileAccess access, FileShare share)
Opens a file in the specified mode with read, write, or read/write access and the specified sharing o...
Definition: FileInfo.cs:404
Implements a T:System.IO.TextReader that reads characters from a byte stream in a particular encoding...
Definition: StreamReader.cs:13
void Encrypt()
Encrypts a file so that only the account used to encrypt the file can decrypt it.
Definition: FileInfo.cs:361
void SetAccessControl(FileSecurity fileSecurity)
Applies access control list (ACL) entries described by a T:System.Security.AccessControl....
Definition: FileInfo.cs:222
static string GetFileName(string path)
Returns the file name and extension of the specified path string.
Definition: Path.cs:914
Describes the source and destination of a given serialized stream, and provides an additional caller-...
StreamReader OpenText()
Creates a T:System.IO.StreamReader with UTF8 encoding that reads from an existing text file.
Definition: FileInfo.cs:235
static FileSecurity GetAccessControl(string path)
Gets a T:System.Security.AccessControl.FileSecurity object that encapsulates the access control list ...
Definition: File.cs:933
void Decrypt()
Decrypts a file that was encrypted by the current account using the M:System.IO.FileInfo....
Definition: FileInfo.cs:348
static void SetAccessControl(string path, FileSecurity fileSecurity)
Applies access control list (ACL) entries described by a T:System.Security.AccessControl....
Definition: File.cs:960
FileStream Open(FileMode mode, FileAccess access)
Opens a file in the specified mode with read, write, or read/write access.
Definition: FileInfo.cs:388
Implements a T:System.IO.TextWriter for writing characters to a stream in a particular encoding....
Definition: StreamWriter.cs:15
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
Provides properties and instance methods for the creation, copying, deletion, moving,...
Definition: FileInfo.cs:14
FileStream Open(FileMode mode)
Opens a file in the specified mode.
Definition: FileInfo.cs:373
FileStream OpenWrite()
Creates a write-only T:System.IO.FileStream.
Definition: FileInfo.cs:424
Exposes static methods for creating, moving, and enumerating through directories and subdirectories....
Definition: Directory.cs:14
Provides a T:System.IO.Stream for a file, supporting both synchronous and asynchronous read and write...
Definition: FileStream.cs:15
FileInfo Replace(string destinationFileName, string destinationBackupFileName, bool ignoreMetadataErrors)
Replaces the contents of a specified file with the file described by the current T:System....
Definition: FileInfo.cs:494
override string ToString()
Returns the path as a string.
Definition: FileInfo.cs:502
FileInfo CopyTo(string destFileName)
Copies an existing file to a new file, disallowing the overwriting of an existing file.
Definition: FileInfo.cs:271
Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks,...
Definition: Marshal.cs:15
FileInfo CopyTo(string destFileName, bool overwrite)
Copies an existing file to a new file, allowing the overwriting of an existing file.
Definition: FileInfo.cs:301
static void Replace(string sourceFileName, string destinationFileName, string destinationBackupFileName)
Replaces the contents of a specified file with the contents of another file, deleting the original fi...
Definition: File.cs:1816
Stores all the data needed to serialize or deserialize an object. This class cannot be inherited.
FileSecurity GetAccessControl(AccessControlSections includeSections)
Gets a T:System.Security.AccessControl.FileSecurity object that encapsulates the specified type of ac...
Definition: FileInfo.cs:211
FileStream Create()
Creates a file.
Definition: FileInfo.cs:317
The exception that is thrown when one of the arguments provided to a method is not valid.
StreamWriter AppendText()
Creates a T:System.IO.StreamWriter that appends text to the file represented by this instance of the ...
Definition: FileInfo.cs:252
FileSecurity GetAccessControl()
Gets a T:System.Security.AccessControl.FileSecurity object that encapsulates the access control list ...
Definition: FileInfo.cs:198
FileAccess
Defines constants for read, write, or read/write access to a file.
Definition: FileAccess.cs:9
FileInfo Replace(string destinationFileName, string destinationBackupFileName)
Replaces the contents of a specified file with the file described by the current T:System....
Definition: FileInfo.cs:478
bool IsReadOnly
Gets or sets a value that determines if the current file is read only.
Definition: FileInfo.cs:93
void Refresh()
Refreshes the state of the object.
StreamWriter CreateText()
Creates a T:System.IO.StreamWriter that writes a new text file.
Definition: FileInfo.cs:245
Specifies that the class can be serialized.
static Encoding UTF8
Gets an encoding for the UTF-8 format.
Definition: Encoding.cs:1023
Provides static methods for the creation, copying, deletion, moving, and opening of a single file,...
Definition: File.cs:14
static void Decrypt(string path)
Decrypts a file that was encrypted by the current account using the M:System.IO.File....
Definition: File.cs:370
static string GetDirectoryName(string path)
Returns the directory information for the specified path string.
Definition: Path.cs:268
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 ...
override bool Exists
Gets a value indicating whether a file exists.
Definition: FileInfo.cs:115
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.
AccessControlSections
Specifies which sections of a security descriptor to save or load.
The P:System.Uri.LocalPath data.
Performs operations on T:System.String instances that contain file or directory path information....
Definition: Path.cs:13
FileStream OpenRead()
Creates a read-only T:System.IO.FileStream.
Definition: FileInfo.cs:415
void MoveTo(string destFileName)
Moves a specified file to a new location, providing the option to specify a new file name.
Definition: FileInfo.cs:445
FileShare
Contains constants for controlling the kind of access other T:System.IO.FileStream objects can have t...
Definition: FileShare.cs:9
string OriginalPath
The path originally specified by the user, whether relative or absolute.
long Length
Gets the size, in bytes, of the current file.
Definition: FileInfo.cs:28