mscorlib(4.0.0.0) API with additions
File.cs
1 using Microsoft.Win32;
2 using Microsoft.Win32.SafeHandles;
5 using System.Security;
8 using System.Text;
9 
10 namespace System.IO
11 {
13  [ComVisible(true)]
14  public static class File
15  {
16  private const int GetFileExInfoStandard = 0;
17 
18  private const int ERROR_INVALID_PARAMETER = 87;
19 
20  private const int ERROR_ACCESS_DENIED = 5;
21 
35  public static StreamReader OpenText(string path)
36  {
37  if (path == null)
38  {
39  throw new ArgumentNullException("path");
40  }
41  return new StreamReader(path);
42  }
43 
56  public static StreamWriter CreateText(string path)
57  {
58  if (path == null)
59  {
60  throw new ArgumentNullException("path");
61  }
62  return new StreamWriter(path, append: false);
63  }
64 
77  public static StreamWriter AppendText(string path)
78  {
79  if (path == null)
80  {
81  throw new ArgumentNullException("path");
82  }
83  return new StreamWriter(path, append: true);
84  }
85 
103  public static void Copy(string sourceFileName, string destFileName)
104  {
105  if (sourceFileName == null)
106  {
107  throw new ArgumentNullException("sourceFileName", Environment.GetResourceString("ArgumentNull_FileName"));
108  }
109  if (destFileName == null)
110  {
111  throw new ArgumentNullException("destFileName", Environment.GetResourceString("ArgumentNull_FileName"));
112  }
113  if (sourceFileName.Length == 0)
114  {
115  throw new ArgumentException(Environment.GetResourceString("Argument_EmptyFileName"), "sourceFileName");
116  }
117  if (destFileName.Length == 0)
118  {
119  throw new ArgumentException(Environment.GetResourceString("Argument_EmptyFileName"), "destFileName");
120  }
121  InternalCopy(sourceFileName, destFileName, overwrite: false, checkHost: true);
122  }
123 
144  public static void Copy(string sourceFileName, string destFileName, bool overwrite)
145  {
146  if (sourceFileName == null)
147  {
148  throw new ArgumentNullException("sourceFileName", Environment.GetResourceString("ArgumentNull_FileName"));
149  }
150  if (destFileName == null)
151  {
152  throw new ArgumentNullException("destFileName", Environment.GetResourceString("ArgumentNull_FileName"));
153  }
154  if (sourceFileName.Length == 0)
155  {
156  throw new ArgumentException(Environment.GetResourceString("Argument_EmptyFileName"), "sourceFileName");
157  }
158  if (destFileName.Length == 0)
159  {
160  throw new ArgumentException(Environment.GetResourceString("Argument_EmptyFileName"), "destFileName");
161  }
162  InternalCopy(sourceFileName, destFileName, overwrite, checkHost: true);
163  }
164 
165  [SecurityCritical]
166  internal static void UnsafeCopy(string sourceFileName, string destFileName, bool overwrite)
167  {
168  if (sourceFileName == null)
169  {
170  throw new ArgumentNullException("sourceFileName", Environment.GetResourceString("ArgumentNull_FileName"));
171  }
172  if (destFileName == null)
173  {
174  throw new ArgumentNullException("destFileName", Environment.GetResourceString("ArgumentNull_FileName"));
175  }
176  if (sourceFileName.Length == 0)
177  {
178  throw new ArgumentException(Environment.GetResourceString("Argument_EmptyFileName"), "sourceFileName");
179  }
180  if (destFileName.Length == 0)
181  {
182  throw new ArgumentException(Environment.GetResourceString("Argument_EmptyFileName"), "destFileName");
183  }
184  InternalCopy(sourceFileName, destFileName, overwrite, checkHost: false);
185  }
186 
187  [SecuritySafeCritical]
188  internal static string InternalCopy(string sourceFileName, string destFileName, bool overwrite, bool checkHost)
189  {
190  string fullPathInternal = Path.GetFullPathInternal(sourceFileName);
191  string fullPathInternal2 = Path.GetFullPathInternal(destFileName);
192  FileIOPermission.QuickDemand(FileIOPermissionAccess.Read, fullPathInternal, checkForDuplicates: false, needFullPath: false);
193  FileIOPermission.QuickDemand(FileIOPermissionAccess.Write, fullPathInternal2, checkForDuplicates: false, needFullPath: false);
194  if (!Win32Native.CopyFile(fullPathInternal, fullPathInternal2, !overwrite))
195  {
196  int lastWin32Error = Marshal.GetLastWin32Error();
197  string maybeFullPath = destFileName;
198  if (lastWin32Error != 80)
199  {
200  using (SafeFileHandle safeFileHandle = Win32Native.UnsafeCreateFile(fullPathInternal, int.MinValue, FileShare.Read, null, FileMode.Open, 0, IntPtr.Zero))
201  {
202  if (safeFileHandle.IsInvalid)
203  {
204  maybeFullPath = sourceFileName;
205  }
206  }
207  if (lastWin32Error == 5 && Directory.InternalExists(fullPathInternal2))
208  {
209  throw new IOException(Environment.GetResourceString("Arg_FileIsDirectory_Name", destFileName), 5, fullPathInternal2);
210  }
211  }
212  __Error.WinIOError(lastWin32Error, maybeFullPath);
213  }
214  return fullPathInternal2;
215  }
216 
231  public static FileStream Create(string path)
232  {
233  return Create(path, 4096);
234  }
235 
251  public static FileStream Create(string path, int bufferSize)
252  {
253  return new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None, bufferSize);
254  }
255 
277  public static FileStream Create(string path, int bufferSize, FileOptions options)
278  {
279  return new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None, bufferSize, options);
280  }
281 
304  public static FileStream Create(string path, int bufferSize, FileOptions options, FileSecurity fileSecurity)
305  {
306  return new FileStream(path, FileMode.Create, FileSystemRights.ReadData | FileSystemRights.WriteData | FileSystemRights.AppendData | FileSystemRights.ReadExtendedAttributes | FileSystemRights.WriteExtendedAttributes | FileSystemRights.ReadAttributes | FileSystemRights.WriteAttributes | FileSystemRights.ReadPermissions, FileShare.None, bufferSize, options, fileSecurity);
307  }
308 
323  [SecuritySafeCritical]
324  public static void Delete(string path)
325  {
326  if (path == null)
327  {
328  throw new ArgumentNullException("path");
329  }
330  InternalDelete(path, checkHost: true);
331  }
332 
333  [SecurityCritical]
334  internal static void UnsafeDelete(string path)
335  {
336  if (path == null)
337  {
338  throw new ArgumentNullException("path");
339  }
340  InternalDelete(path, checkHost: false);
341  }
342 
343  [SecurityCritical]
344  internal static void InternalDelete(string path, bool checkHost)
345  {
346  string fullPathInternal = Path.GetFullPathInternal(path);
347  FileIOPermission.QuickDemand(FileIOPermissionAccess.Write, fullPathInternal, checkForDuplicates: false, needFullPath: false);
348  if (!Win32Native.DeleteFile(fullPathInternal))
349  {
350  int lastWin32Error = Marshal.GetLastWin32Error();
351  if (lastWin32Error != 2)
352  {
353  __Error.WinIOError(lastWin32Error, fullPathInternal);
354  }
355  }
356  }
357 
369  [SecuritySafeCritical]
370  public static void Decrypt(string path)
371  {
372  if (path == null)
373  {
374  throw new ArgumentNullException("path");
375  }
376  string fullPathInternal = Path.GetFullPathInternal(path);
377  FileIOPermission.QuickDemand(FileIOPermissionAccess.Read | FileIOPermissionAccess.Write, fullPathInternal, checkForDuplicates: false, needFullPath: false);
378  if (Win32Native.DecryptFile(fullPathInternal, 0))
379  {
380  return;
381  }
382  int lastWin32Error = Marshal.GetLastWin32Error();
383  if (lastWin32Error == 5)
384  {
385  DriveInfo driveInfo = new DriveInfo(Path.GetPathRoot(fullPathInternal));
386  if (!string.Equals("NTFS", driveInfo.DriveFormat))
387  {
388  throw new NotSupportedException(Environment.GetResourceString("NotSupported_EncryptionNeedsNTFS"));
389  }
390  }
391  __Error.WinIOError(lastWin32Error, fullPathInternal);
392  }
393 
405  [SecuritySafeCritical]
406  public static void Encrypt(string path)
407  {
408  if (path == null)
409  {
410  throw new ArgumentNullException("path");
411  }
412  string fullPathInternal = Path.GetFullPathInternal(path);
413  FileIOPermission.QuickDemand(FileIOPermissionAccess.Read | FileIOPermissionAccess.Write, fullPathInternal, checkForDuplicates: false, needFullPath: false);
414  if (Win32Native.EncryptFile(fullPathInternal))
415  {
416  return;
417  }
418  int lastWin32Error = Marshal.GetLastWin32Error();
419  if (lastWin32Error == 5)
420  {
421  DriveInfo driveInfo = new DriveInfo(Path.GetPathRoot(fullPathInternal));
422  if (!string.Equals("NTFS", driveInfo.DriveFormat))
423  {
424  throw new NotSupportedException(Environment.GetResourceString("NotSupported_EncryptionNeedsNTFS"));
425  }
426  }
427  __Error.WinIOError(lastWin32Error, fullPathInternal);
428  }
429 
434  [SecuritySafeCritical]
435  public static bool Exists(string path)
436  {
437  return InternalExistsHelper(path, checkHost: true);
438  }
439 
440  [SecurityCritical]
441  internal static bool UnsafeExists(string path)
442  {
443  return InternalExistsHelper(path, checkHost: false);
444  }
445 
446  [SecurityCritical]
447  private static bool InternalExistsHelper(string path, bool checkHost)
448  {
449  try
450  {
451  if (path == null)
452  {
453  return false;
454  }
455  if (path.Length == 0)
456  {
457  return false;
458  }
459  path = Path.GetFullPathInternal(path);
460  if (path.Length > 0 && Path.IsDirectorySeparator(path[path.Length - 1]))
461  {
462  return false;
463  }
464  FileIOPermission.QuickDemand(FileIOPermissionAccess.Read, path, checkForDuplicates: false, needFullPath: false);
465  return InternalExists(path);
466  }
467  catch (ArgumentException)
468  {
469  }
470  catch (NotSupportedException)
471  {
472  }
473  catch (SecurityException)
474  {
475  }
476  catch (IOException)
477  {
478  }
479  catch (UnauthorizedAccessException)
480  {
481  }
482  return false;
483  }
484 
485  [SecurityCritical]
486  internal static bool InternalExists(string path)
487  {
488  Win32Native.WIN32_FILE_ATTRIBUTE_DATA data = default(Win32Native.WIN32_FILE_ATTRIBUTE_DATA);
489  if (FillAttributeInfo(path, ref data, tryagain: false, returnErrorOnNotFound: true) == 0 && data.fileAttributes != -1)
490  {
491  return (data.fileAttributes & 0x10) == 0;
492  }
493  return false;
494  }
495 
516  public static FileStream Open(string path, FileMode mode)
517  {
518  return Open(path, mode, (mode == FileMode.Append) ? FileAccess.Write : FileAccess.ReadWrite, FileShare.None);
519  }
520 
543  public static FileStream Open(string path, FileMode mode, FileAccess access)
544  {
545  return Open(path, mode, access, FileShare.None);
546  }
547 
571  public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share)
572  {
573  return new FileStream(path, mode, access, share);
574  }
575 
591  public static void SetCreationTime(string path, DateTime creationTime)
592  {
593  SetCreationTimeUtc(path, creationTime.ToUniversalTime());
594  }
595 
611  [SecuritySafeCritical]
612  public unsafe static void SetCreationTimeUtc(string path, DateTime creationTimeUtc)
613  {
614  SafeFileHandle handle;
615  using (OpenFile(path, FileAccess.Write, out handle))
616  {
617  Win32Native.FILE_TIME fILE_TIME = new Win32Native.FILE_TIME(creationTimeUtc.ToFileTimeUtc());
618  if (!Win32Native.SetFileTime(handle, &fILE_TIME, null, null))
619  {
620  int lastWin32Error = Marshal.GetLastWin32Error();
621  __Error.WinIOError(lastWin32Error, path);
622  }
623  }
624  }
625 
637  [SecuritySafeCritical]
638  public static DateTime GetCreationTime(string path)
639  {
640  return InternalGetCreationTimeUtc(path, checkHost: true).ToLocalTime();
641  }
642 
654  [SecuritySafeCritical]
655  public static DateTime GetCreationTimeUtc(string path)
656  {
657  return InternalGetCreationTimeUtc(path, checkHost: false);
658  }
659 
660  [SecurityCritical]
661  private static DateTime InternalGetCreationTimeUtc(string path, bool checkHost)
662  {
663  string fullPathInternal = Path.GetFullPathInternal(path);
664  FileIOPermission.QuickDemand(FileIOPermissionAccess.Read, fullPathInternal, checkForDuplicates: false, needFullPath: false);
665  Win32Native.WIN32_FILE_ATTRIBUTE_DATA data = default(Win32Native.WIN32_FILE_ATTRIBUTE_DATA);
666  int num = FillAttributeInfo(fullPathInternal, ref data, tryagain: false, returnErrorOnNotFound: false);
667  if (num != 0)
668  {
669  __Error.WinIOError(num, fullPathInternal);
670  }
671  long fileTime = (long)(((ulong)data.ftCreationTimeHigh << 32) | data.ftCreationTimeLow);
672  return DateTime.FromFileTimeUtc(fileTime);
673  }
674 
689  public static void SetLastAccessTime(string path, DateTime lastAccessTime)
690  {
691  SetLastAccessTimeUtc(path, lastAccessTime.ToUniversalTime());
692  }
693 
708  [SecuritySafeCritical]
709  public unsafe static void SetLastAccessTimeUtc(string path, DateTime lastAccessTimeUtc)
710  {
711  SafeFileHandle handle;
712  using (OpenFile(path, FileAccess.Write, out handle))
713  {
714  Win32Native.FILE_TIME fILE_TIME = new Win32Native.FILE_TIME(lastAccessTimeUtc.ToFileTimeUtc());
715  if (!Win32Native.SetFileTime(handle, null, &fILE_TIME, null))
716  {
717  int lastWin32Error = Marshal.GetLastWin32Error();
718  __Error.WinIOError(lastWin32Error, path);
719  }
720  }
721  }
722 
734  [SecuritySafeCritical]
735  public static DateTime GetLastAccessTime(string path)
736  {
737  return InternalGetLastAccessTimeUtc(path, checkHost: true).ToLocalTime();
738  }
739 
751  [SecuritySafeCritical]
752  public static DateTime GetLastAccessTimeUtc(string path)
753  {
754  return InternalGetLastAccessTimeUtc(path, checkHost: false);
755  }
756 
757  [SecurityCritical]
758  private static DateTime InternalGetLastAccessTimeUtc(string path, bool checkHost)
759  {
760  string fullPathInternal = Path.GetFullPathInternal(path);
761  FileIOPermission.QuickDemand(FileIOPermissionAccess.Read, fullPathInternal, checkForDuplicates: false, needFullPath: false);
762  Win32Native.WIN32_FILE_ATTRIBUTE_DATA data = default(Win32Native.WIN32_FILE_ATTRIBUTE_DATA);
763  int num = FillAttributeInfo(fullPathInternal, ref data, tryagain: false, returnErrorOnNotFound: false);
764  if (num != 0)
765  {
766  __Error.WinIOError(num, fullPathInternal);
767  }
768  long fileTime = (long)(((ulong)data.ftLastAccessTimeHigh << 32) | data.ftLastAccessTimeLow);
769  return DateTime.FromFileTimeUtc(fileTime);
770  }
771 
786  public static void SetLastWriteTime(string path, DateTime lastWriteTime)
787  {
788  SetLastWriteTimeUtc(path, lastWriteTime.ToUniversalTime());
789  }
790 
805  [SecuritySafeCritical]
806  public unsafe static void SetLastWriteTimeUtc(string path, DateTime lastWriteTimeUtc)
807  {
808  SafeFileHandle handle;
809  using (OpenFile(path, FileAccess.Write, out handle))
810  {
811  Win32Native.FILE_TIME fILE_TIME = new Win32Native.FILE_TIME(lastWriteTimeUtc.ToFileTimeUtc());
812  if (!Win32Native.SetFileTime(handle, null, null, &fILE_TIME))
813  {
814  int lastWin32Error = Marshal.GetLastWin32Error();
815  __Error.WinIOError(lastWin32Error, path);
816  }
817  }
818  }
819 
831  [SecuritySafeCritical]
832  public static DateTime GetLastWriteTime(string path)
833  {
834  return InternalGetLastWriteTimeUtc(path, checkHost: true).ToLocalTime();
835  }
836 
848  [SecuritySafeCritical]
849  public static DateTime GetLastWriteTimeUtc(string path)
850  {
851  return InternalGetLastWriteTimeUtc(path, checkHost: false);
852  }
853 
854  [SecurityCritical]
855  private static DateTime InternalGetLastWriteTimeUtc(string path, bool checkHost)
856  {
857  string fullPathInternal = Path.GetFullPathInternal(path);
858  FileIOPermission.QuickDemand(FileIOPermissionAccess.Read, fullPathInternal, checkForDuplicates: false, needFullPath: false);
859  Win32Native.WIN32_FILE_ATTRIBUTE_DATA data = default(Win32Native.WIN32_FILE_ATTRIBUTE_DATA);
860  int num = FillAttributeInfo(fullPathInternal, ref data, tryagain: false, returnErrorOnNotFound: false);
861  if (num != 0)
862  {
863  __Error.WinIOError(num, fullPathInternal);
864  }
865  long fileTime = (long)(((ulong)data.ftLastWriteTimeHigh << 32) | data.ftLastWriteTimeLow);
866  return DateTime.FromFileTimeUtc(fileTime);
867  }
868 
883  [SecuritySafeCritical]
884  public static FileAttributes GetAttributes(string path)
885  {
886  string fullPathInternal = Path.GetFullPathInternal(path);
887  FileIOPermission.QuickDemand(FileIOPermissionAccess.Read, fullPathInternal, checkForDuplicates: false, needFullPath: false);
888  Win32Native.WIN32_FILE_ATTRIBUTE_DATA data = default(Win32Native.WIN32_FILE_ATTRIBUTE_DATA);
889  int num = FillAttributeInfo(fullPathInternal, ref data, tryagain: false, returnErrorOnNotFound: true);
890  if (num != 0)
891  {
892  __Error.WinIOError(num, fullPathInternal);
893  }
894  return (FileAttributes)data.fileAttributes;
895  }
896 
910  [SecuritySafeCritical]
911  public static void SetAttributes(string path, FileAttributes fileAttributes)
912  {
913  string fullPathInternal = Path.GetFullPathInternal(path);
914  FileIOPermission.QuickDemand(FileIOPermissionAccess.Write, fullPathInternal, checkForDuplicates: false, needFullPath: false);
915  if (!Win32Native.SetFileAttributes(fullPathInternal, (int)fileAttributes))
916  {
917  int lastWin32Error = Marshal.GetLastWin32Error();
918  if (lastWin32Error == 87)
919  {
920  throw new ArgumentException(Environment.GetResourceString("Arg_InvalidFileAttrs"));
921  }
922  __Error.WinIOError(lastWin32Error, fullPathInternal);
923  }
924  }
925 
933  public static FileSecurity GetAccessControl(string path)
934  {
936  }
937 
946  public static FileSecurity GetAccessControl(string path, AccessControlSections includeSections)
947  {
948  return new FileSecurity(path, includeSections);
949  }
950 
959  [SecuritySafeCritical]
960  public static void SetAccessControl(string path, FileSecurity fileSecurity)
961  {
962  if (fileSecurity == null)
963  {
964  throw new ArgumentNullException("fileSecurity");
965  }
966  string fullPathInternal = Path.GetFullPathInternal(path);
967  fileSecurity.Persist(fullPathInternal);
968  }
969 
985  public static FileStream OpenRead(string path)
986  {
987  return new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
988  }
989 
1003  public static FileStream OpenWrite(string path)
1004  {
1005  return new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
1006  }
1007 
1025  [SecuritySafeCritical]
1026  public static string ReadAllText(string path)
1027  {
1028  if (path == null)
1029  {
1030  throw new ArgumentNullException("path");
1031  }
1032  if (path.Length == 0)
1033  {
1034  throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
1035  }
1036  return InternalReadAllText(path, Encoding.UTF8, checkHost: true);
1037  }
1038 
1057  [SecuritySafeCritical]
1058  public static string ReadAllText(string path, Encoding encoding)
1059  {
1060  if (path == null)
1061  {
1062  throw new ArgumentNullException("path");
1063  }
1064  if (encoding == null)
1065  {
1066  throw new ArgumentNullException("encoding");
1067  }
1068  if (path.Length == 0)
1069  {
1070  throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
1071  }
1072  return InternalReadAllText(path, encoding, checkHost: true);
1073  }
1074 
1075  [SecurityCritical]
1076  internal static string UnsafeReadAllText(string path)
1077  {
1078  if (path == null)
1079  {
1080  throw new ArgumentNullException("path");
1081  }
1082  if (path.Length == 0)
1083  {
1084  throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
1085  }
1086  return InternalReadAllText(path, Encoding.UTF8, checkHost: false);
1087  }
1088 
1089  [SecurityCritical]
1090  private static string InternalReadAllText(string path, Encoding encoding, bool checkHost)
1091  {
1092  using (StreamReader streamReader = new StreamReader(path, encoding, detectEncodingFromByteOrderMarks: true, StreamReader.DefaultBufferSize, checkHost))
1093  {
1094  return streamReader.ReadToEnd();
1095  }
1096  }
1097 
1114  [SecuritySafeCritical]
1115  public static void WriteAllText(string path, string contents)
1116  {
1117  if (path == null)
1118  {
1119  throw new ArgumentNullException("path");
1120  }
1121  if (path.Length == 0)
1122  {
1123  throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
1124  }
1125  InternalWriteAllText(path, contents, StreamWriter.UTF8NoBOM, checkHost: true);
1126  }
1127 
1145  [SecuritySafeCritical]
1146  public static void WriteAllText(string path, string contents, Encoding encoding)
1147  {
1148  if (path == null)
1149  {
1150  throw new ArgumentNullException("path");
1151  }
1152  if (encoding == null)
1153  {
1154  throw new ArgumentNullException("encoding");
1155  }
1156  if (path.Length == 0)
1157  {
1158  throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
1159  }
1160  InternalWriteAllText(path, contents, encoding, checkHost: true);
1161  }
1162 
1163  [SecurityCritical]
1164  internal static void UnsafeWriteAllText(string path, string contents)
1165  {
1166  if (path == null)
1167  {
1168  throw new ArgumentNullException("path");
1169  }
1170  if (path.Length == 0)
1171  {
1172  throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
1173  }
1174  InternalWriteAllText(path, contents, StreamWriter.UTF8NoBOM, checkHost: false);
1175  }
1176 
1177  [SecurityCritical]
1178  private static void InternalWriteAllText(string path, string contents, Encoding encoding, bool checkHost)
1179  {
1180  using (StreamWriter streamWriter = new StreamWriter(path, append: false, encoding, 1024, checkHost))
1181  {
1182  streamWriter.Write(contents);
1183  }
1184  }
1185 
1202  [SecuritySafeCritical]
1203  public static byte[] ReadAllBytes(string path)
1204  {
1205  return InternalReadAllBytes(path, checkHost: true);
1206  }
1207 
1208  [SecurityCritical]
1209  internal static byte[] UnsafeReadAllBytes(string path)
1210  {
1211  return InternalReadAllBytes(path, checkHost: false);
1212  }
1213 
1214  [SecurityCritical]
1215  private static byte[] InternalReadAllBytes(string path, bool checkHost)
1216  {
1217  using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.None, Path.GetFileName(path), bFromProxy: false, useLongPath: false, checkHost))
1218  {
1219  int num = 0;
1220  long length = fileStream.Length;
1221  if (length > int.MaxValue)
1222  {
1223  throw new IOException(Environment.GetResourceString("IO.IO_FileTooLong2GB"));
1224  }
1225  int num2 = (int)length;
1226  byte[] array = new byte[num2];
1227  while (num2 > 0)
1228  {
1229  int num3 = fileStream.Read(array, num, num2);
1230  if (num3 == 0)
1231  {
1232  __Error.EndOfFile();
1233  }
1234  num += num3;
1235  num2 -= num3;
1236  }
1237  return array;
1238  }
1239  }
1240 
1257  [SecuritySafeCritical]
1258  public static void WriteAllBytes(string path, byte[] bytes)
1259  {
1260  if (path == null)
1261  {
1262  throw new ArgumentNullException("path", Environment.GetResourceString("ArgumentNull_Path"));
1263  }
1264  if (path.Length == 0)
1265  {
1266  throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
1267  }
1268  if (bytes == null)
1269  {
1270  throw new ArgumentNullException("bytes");
1271  }
1272  InternalWriteAllBytes(path, bytes, checkHost: true);
1273  }
1274 
1275  [SecurityCritical]
1276  internal static void UnsafeWriteAllBytes(string path, byte[] bytes)
1277  {
1278  if (path == null)
1279  {
1280  throw new ArgumentNullException("path", Environment.GetResourceString("ArgumentNull_Path"));
1281  }
1282  if (path.Length == 0)
1283  {
1284  throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
1285  }
1286  if (bytes == null)
1287  {
1288  throw new ArgumentNullException("bytes");
1289  }
1290  InternalWriteAllBytes(path, bytes, checkHost: false);
1291  }
1292 
1293  [SecurityCritical]
1294  private static void InternalWriteAllBytes(string path, byte[] bytes, bool checkHost)
1295  {
1296  using (FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read, 4096, FileOptions.None, Path.GetFileName(path), bFromProxy: false, useLongPath: false, checkHost))
1297  {
1298  fileStream.Write(bytes, 0, bytes.Length);
1299  }
1300  }
1301 
1319  public static string[] ReadAllLines(string path)
1320  {
1321  if (path == null)
1322  {
1323  throw new ArgumentNullException("path");
1324  }
1325  if (path.Length == 0)
1326  {
1327  throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
1328  }
1329  return InternalReadAllLines(path, Encoding.UTF8);
1330  }
1331 
1350  public static string[] ReadAllLines(string path, Encoding encoding)
1351  {
1352  if (path == null)
1353  {
1354  throw new ArgumentNullException("path");
1355  }
1356  if (encoding == null)
1357  {
1358  throw new ArgumentNullException("encoding");
1359  }
1360  if (path.Length == 0)
1361  {
1362  throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
1363  }
1364  return InternalReadAllLines(path, encoding);
1365  }
1366 
1367  private static string[] InternalReadAllLines(string path, Encoding encoding)
1368  {
1369  List<string> list = new List<string>();
1370  using (StreamReader streamReader = new StreamReader(path, encoding))
1371  {
1372  string item;
1373  while ((item = streamReader.ReadLine()) != null)
1374  {
1375  list.Add(item);
1376  }
1377  }
1378  return list.ToArray();
1379  }
1380 
1398  public static IEnumerable<string> ReadLines(string path)
1399  {
1400  if (path == null)
1401  {
1402  throw new ArgumentNullException("path");
1403  }
1404  if (path.Length == 0)
1405  {
1406  throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"), "path");
1407  }
1408  return ReadLinesIterator.CreateIterator(path, Encoding.UTF8);
1409  }
1410 
1429  public static IEnumerable<string> ReadLines(string path, Encoding encoding)
1430  {
1431  if (path == null)
1432  {
1433  throw new ArgumentNullException("path");
1434  }
1435  if (encoding == null)
1436  {
1437  throw new ArgumentNullException("encoding");
1438  }
1439  if (path.Length == 0)
1440  {
1441  throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"), "path");
1442  }
1443  return ReadLinesIterator.CreateIterator(path, encoding);
1444  }
1445 
1461  public static void WriteAllLines(string path, string[] contents)
1462  {
1463  if (path == null)
1464  {
1465  throw new ArgumentNullException("path");
1466  }
1467  if (contents == null)
1468  {
1469  throw new ArgumentNullException("contents");
1470  }
1471  if (path.Length == 0)
1472  {
1473  throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
1474  }
1475  InternalWriteAllLines(new StreamWriter(path, append: false, StreamWriter.UTF8NoBOM), contents);
1476  }
1477 
1494  public static void WriteAllLines(string path, string[] contents, Encoding encoding)
1495  {
1496  if (path == null)
1497  {
1498  throw new ArgumentNullException("path");
1499  }
1500  if (contents == null)
1501  {
1502  throw new ArgumentNullException("contents");
1503  }
1504  if (encoding == null)
1505  {
1506  throw new ArgumentNullException("encoding");
1507  }
1508  if (path.Length == 0)
1509  {
1510  throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
1511  }
1512  InternalWriteAllLines(new StreamWriter(path, append: false, encoding), contents);
1513  }
1514 
1532  public static void WriteAllLines(string path, IEnumerable<string> contents)
1533  {
1534  if (path == null)
1535  {
1536  throw new ArgumentNullException("path");
1537  }
1538  if (contents == null)
1539  {
1540  throw new ArgumentNullException("contents");
1541  }
1542  if (path.Length == 0)
1543  {
1544  throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
1545  }
1546  InternalWriteAllLines(new StreamWriter(path, append: false, StreamWriter.UTF8NoBOM), contents);
1547  }
1548 
1567  public static void WriteAllLines(string path, IEnumerable<string> contents, Encoding encoding)
1568  {
1569  if (path == null)
1570  {
1571  throw new ArgumentNullException("path");
1572  }
1573  if (contents == null)
1574  {
1575  throw new ArgumentNullException("contents");
1576  }
1577  if (encoding == null)
1578  {
1579  throw new ArgumentNullException("encoding");
1580  }
1581  if (path.Length == 0)
1582  {
1583  throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
1584  }
1585  InternalWriteAllLines(new StreamWriter(path, append: false, encoding), contents);
1586  }
1587 
1588  private static void InternalWriteAllLines(TextWriter writer, IEnumerable<string> contents)
1589  {
1590  using (writer)
1591  {
1592  foreach (string content in contents)
1593  {
1594  writer.WriteLine(content);
1595  }
1596  }
1597  }
1598 
1615  public static void AppendAllText(string path, string contents)
1616  {
1617  if (path == null)
1618  {
1619  throw new ArgumentNullException("path");
1620  }
1621  if (path.Length == 0)
1622  {
1623  throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
1624  }
1625  InternalAppendAllText(path, contents, StreamWriter.UTF8NoBOM);
1626  }
1627 
1645  public static void AppendAllText(string path, string contents, Encoding encoding)
1646  {
1647  if (path == null)
1648  {
1649  throw new ArgumentNullException("path");
1650  }
1651  if (encoding == null)
1652  {
1653  throw new ArgumentNullException("encoding");
1654  }
1655  if (path.Length == 0)
1656  {
1657  throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
1658  }
1659  InternalAppendAllText(path, contents, encoding);
1660  }
1661 
1662  private static void InternalAppendAllText(string path, string contents, Encoding encoding)
1663  {
1664  using (StreamWriter streamWriter = new StreamWriter(path, append: true, encoding))
1665  {
1666  streamWriter.Write(contents);
1667  }
1668  }
1669 
1688  public static void AppendAllLines(string path, IEnumerable<string> contents)
1689  {
1690  if (path == null)
1691  {
1692  throw new ArgumentNullException("path");
1693  }
1694  if (contents == null)
1695  {
1696  throw new ArgumentNullException("contents");
1697  }
1698  if (path.Length == 0)
1699  {
1700  throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
1701  }
1702  InternalWriteAllLines(new StreamWriter(path, append: true, StreamWriter.UTF8NoBOM), contents);
1703  }
1704 
1724  public static void AppendAllLines(string path, IEnumerable<string> contents, Encoding encoding)
1725  {
1726  if (path == null)
1727  {
1728  throw new ArgumentNullException("path");
1729  }
1730  if (contents == null)
1731  {
1732  throw new ArgumentNullException("contents");
1733  }
1734  if (encoding == null)
1735  {
1736  throw new ArgumentNullException("encoding");
1737  }
1738  if (path.Length == 0)
1739  {
1740  throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
1741  }
1742  InternalWriteAllLines(new StreamWriter(path, append: true, encoding), contents);
1743  }
1744 
1759  [SecuritySafeCritical]
1760  public static void Move(string sourceFileName, string destFileName)
1761  {
1762  InternalMove(sourceFileName, destFileName, checkHost: true);
1763  }
1764 
1765  [SecurityCritical]
1766  internal static void UnsafeMove(string sourceFileName, string destFileName)
1767  {
1768  InternalMove(sourceFileName, destFileName, checkHost: false);
1769  }
1770 
1771  [SecurityCritical]
1772  private static void InternalMove(string sourceFileName, string destFileName, bool checkHost)
1773  {
1774  if (sourceFileName == null)
1775  {
1776  throw new ArgumentNullException("sourceFileName", Environment.GetResourceString("ArgumentNull_FileName"));
1777  }
1778  if (destFileName == null)
1779  {
1780  throw new ArgumentNullException("destFileName", Environment.GetResourceString("ArgumentNull_FileName"));
1781  }
1782  if (sourceFileName.Length == 0)
1783  {
1784  throw new ArgumentException(Environment.GetResourceString("Argument_EmptyFileName"), "sourceFileName");
1785  }
1786  if (destFileName.Length == 0)
1787  {
1788  throw new ArgumentException(Environment.GetResourceString("Argument_EmptyFileName"), "destFileName");
1789  }
1790  string fullPathInternal = Path.GetFullPathInternal(sourceFileName);
1791  string fullPathInternal2 = Path.GetFullPathInternal(destFileName);
1792  FileIOPermission.QuickDemand(FileIOPermissionAccess.Read | FileIOPermissionAccess.Write, fullPathInternal, checkForDuplicates: false, needFullPath: false);
1793  FileIOPermission.QuickDemand(FileIOPermissionAccess.Write, fullPathInternal2, checkForDuplicates: false, needFullPath: false);
1794  if (!InternalExists(fullPathInternal))
1795  {
1796  __Error.WinIOError(2, fullPathInternal);
1797  }
1798  if (!Win32Native.MoveFile(fullPathInternal, fullPathInternal2))
1799  {
1800  __Error.WinIOError();
1801  }
1802  }
1803 
1816  public static void Replace(string sourceFileName, string destinationFileName, string destinationBackupFileName)
1817  {
1818  if (sourceFileName == null)
1819  {
1820  throw new ArgumentNullException("sourceFileName");
1821  }
1822  if (destinationFileName == null)
1823  {
1824  throw new ArgumentNullException("destinationFileName");
1825  }
1826  InternalReplace(sourceFileName, destinationFileName, destinationBackupFileName, ignoreMetadataErrors: false);
1827  }
1828 
1843  public static void Replace(string sourceFileName, string destinationFileName, string destinationBackupFileName, bool ignoreMetadataErrors)
1844  {
1845  if (sourceFileName == null)
1846  {
1847  throw new ArgumentNullException("sourceFileName");
1848  }
1849  if (destinationFileName == null)
1850  {
1851  throw new ArgumentNullException("destinationFileName");
1852  }
1853  InternalReplace(sourceFileName, destinationFileName, destinationBackupFileName, ignoreMetadataErrors);
1854  }
1855 
1856  [SecuritySafeCritical]
1857  private static void InternalReplace(string sourceFileName, string destinationFileName, string destinationBackupFileName, bool ignoreMetadataErrors)
1858  {
1859  string fullPathInternal = Path.GetFullPathInternal(sourceFileName);
1860  string fullPathInternal2 = Path.GetFullPathInternal(destinationFileName);
1861  string text = null;
1862  if (destinationBackupFileName != null)
1863  {
1864  text = Path.GetFullPathInternal(destinationBackupFileName);
1865  }
1866  if (CodeAccessSecurityEngine.QuickCheckForAllDemands())
1867  {
1868  FileIOPermission.EmulateFileIOPermissionChecks(fullPathInternal);
1869  FileIOPermission.EmulateFileIOPermissionChecks(fullPathInternal2);
1870  if (text != null)
1871  {
1872  FileIOPermission.EmulateFileIOPermissionChecks(text);
1873  }
1874  }
1875  else
1876  {
1877  FileIOPermission fileIOPermission = new FileIOPermission(FileIOPermissionAccess.Read | FileIOPermissionAccess.Write, new string[2]
1878  {
1879  fullPathInternal,
1880  fullPathInternal2
1881  });
1882  if (text != null)
1883  {
1884  fileIOPermission.AddPathList(FileIOPermissionAccess.Write, text);
1885  }
1886  fileIOPermission.Demand();
1887  }
1888  int num = 1;
1889  if (ignoreMetadataErrors)
1890  {
1891  num |= 2;
1892  }
1893  if (!Win32Native.ReplaceFile(fullPathInternal2, fullPathInternal, text, num, IntPtr.Zero, IntPtr.Zero))
1894  {
1895  __Error.WinIOError();
1896  }
1897  }
1898 
1899  [SecurityCritical]
1900  internal static int FillAttributeInfo(string path, ref Win32Native.WIN32_FILE_ATTRIBUTE_DATA data, bool tryagain, bool returnErrorOnNotFound)
1901  {
1902  int num = 0;
1903  if (tryagain)
1904  {
1905  Win32Native.WIN32_FIND_DATA wIN32_FIND_DATA = new Win32Native.WIN32_FIND_DATA();
1906  string fileName = path.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
1907  int errorMode = Win32Native.SetErrorMode(1);
1908  try
1909  {
1910  bool flag = false;
1911  SafeFindHandle safeFindHandle = Win32Native.FindFirstFile(fileName, wIN32_FIND_DATA);
1912  try
1913  {
1914  if (safeFindHandle.IsInvalid)
1915  {
1916  flag = true;
1917  num = Marshal.GetLastWin32Error();
1918  if ((num == 2 || num == 3 || num == 21) && !returnErrorOnNotFound)
1919  {
1920  num = 0;
1921  data.fileAttributes = -1;
1922  }
1923  return num;
1924  }
1925  }
1926  finally
1927  {
1928  try
1929  {
1930  safeFindHandle.Close();
1931  }
1932  catch
1933  {
1934  if (!flag)
1935  {
1936  __Error.WinIOError();
1937  }
1938  }
1939  }
1940  }
1941  finally
1942  {
1943  Win32Native.SetErrorMode(errorMode);
1944  }
1945  data.PopulateFrom(wIN32_FIND_DATA);
1946  }
1947  else
1948  {
1949  bool flag2 = false;
1950  int errorMode2 = Win32Native.SetErrorMode(1);
1951  try
1952  {
1953  flag2 = Win32Native.GetFileAttributesEx(path, 0, ref data);
1954  }
1955  finally
1956  {
1957  Win32Native.SetErrorMode(errorMode2);
1958  }
1959  if (!flag2)
1960  {
1961  num = Marshal.GetLastWin32Error();
1962  if (num != 2 && num != 3 && num != 21)
1963  {
1964  return FillAttributeInfo(path, ref data, tryagain: true, returnErrorOnNotFound);
1965  }
1966  if (!returnErrorOnNotFound)
1967  {
1968  num = 0;
1969  data.fileAttributes = -1;
1970  }
1971  }
1972  }
1973  return num;
1974  }
1975 
1976  [SecurityCritical]
1977  private static FileStream OpenFile(string path, FileAccess access, out SafeFileHandle handle)
1978  {
1979  FileStream fileStream = new FileStream(path, FileMode.Open, access, FileShare.ReadWrite, 1);
1980  handle = fileStream.SafeFileHandle;
1981  if (handle.IsInvalid)
1982  {
1983  int num = Marshal.GetLastWin32Error();
1984  string fullPathInternal = Path.GetFullPathInternal(path);
1985  if (num == 3 && fullPathInternal.Equals(Directory.GetDirectoryRoot(fullPathInternal)))
1986  {
1987  num = 5;
1988  }
1989  __Error.WinIOError(num, path);
1990  }
1991  return fileStream;
1992  }
1993  }
1994 }
Represents a character encoding.To browse the .NET Framework source code for this type,...
Definition: Encoding.cs:15
FileSystemRights
Defines the access rights to use when creating access and audit rules.
static DateTime GetCreationTime(string path)
Returns the creation date and time of the specified file or directory.
Definition: File.cs:638
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
static void SetAttributes(string path, FileAttributes fileAttributes)
Sets the specified T:System.IO.FileAttributes of the file on the specified path.
Definition: File.cs:911
FileIOPermissionAccess
Specifies the type of file access requested.
static bool Exists(string path)
Determines whether the specified file exists.
Definition: File.cs:435
static FileSecurity GetAccessControl(string path, AccessControlSections includeSections)
Gets a T:System.Security.AccessControl.FileSecurity object that encapsulates the specified type of ac...
Definition: File.cs:946
static StreamWriter CreateText(string path)
Creates or opens a file for writing UTF-8 encoded text.
Definition: File.cs:56
FileOptions
Represents advanced options for creating a T:System.IO.FileStream object.
Definition: FileOptions.cs:9
static string ReadAllText(string path, Encoding encoding)
Opens a file, reads all lines of the file with the specified encoding, and then closes the file.
Definition: File.cs:1058
static FileStream Create(string path)
Creates or overwrites a file in the specified path.
Definition: File.cs:231
static void WriteAllText(string path, string contents, Encoding encoding)
Creates a new file, writes the specified string to the file using the specified encoding,...
Definition: File.cs:1146
long ToFileTimeUtc()
Converts the value of the current T:System.DateTime object to a Windows file time.
Definition: DateTime.cs:1446
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
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
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.
Definition: __Canon.cs:3
static void AppendAllText(string path, string contents, Encoding encoding)
Appends the specified string to the file, creating the file if it does not already exist.
Definition: File.cs:1645
static FileStream OpenWrite(string path)
Opens an existing file or creates a new file for writing.
Definition: File.cs:1003
static string GetPathRoot(string path)
Gets the root directory information of the specified path.
Definition: Path.cs:961
Implements a T:System.IO.TextReader that reads characters from a byte stream in a particular encoding...
Definition: StreamReader.cs:13
static FileStream Create(string path, int bufferSize)
Creates or overwrites the specified file.
Definition: File.cs:251
static FileStream OpenRead(string path)
Opens an existing file for reading.
Definition: File.cs:985
static DateTime GetLastWriteTime(string path)
Returns the date and time the specified file or directory was last written to.
Definition: File.cs:832
Represents an instant in time, typically expressed as a date and time of day. To browse the ....
Definition: DateTime.cs:13
static IEnumerable< string > ReadLines(string path, Encoding encoding)
Read the lines of a file that has a specified encoding.
Definition: File.cs:1429
override string ReadLine()
Reads a line of characters from the current stream and returns the data as a string.
static string [] ReadAllLines(string path, Encoding encoding)
Opens a file, reads all lines of the file with the specified encoding, and then closes the file.
Definition: File.cs:1350
static DateTime GetCreationTimeUtc(string path)
Returns the creation date and time, in coordinated universal time (UTC), of the specified file or dir...
Definition: File.cs:655
static void SetCreationTime(string path, DateTime creationTime)
Sets the date and time the file was created.
Definition: File.cs:591
static DateTime GetLastAccessTimeUtc(string path)
Returns the date and time, in coordinated universal time (UTC), that the specified file or directory ...
Definition: File.cs:752
static FileSecurity GetAccessControl(string path)
Gets a T:System.Security.AccessControl.FileSecurity object that encapsulates the access control list ...
Definition: File.cs:933
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
static void Replace(string sourceFileName, string destinationFileName, string destinationBackupFileName, bool ignoreMetadataErrors)
Replaces the contents of a specified file with the contents of another file, deleting the original fi...
Definition: File.cs:1843
static void WriteAllLines(string path, string[] contents)
Creates a new file, write the specified string array to the file, and then closes the file.
Definition: File.cs:1461
static FileStream Create(string path, int bufferSize, FileOptions options, FileSecurity fileSecurity)
Creates or overwrites the specified file with the specified buffer size, file options,...
Definition: File.cs:304
static void SetAccessControl(string path, FileSecurity fileSecurity)
Applies access control list (ACL) entries described by a T:System.Security.AccessControl....
Definition: File.cs:960
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
void AddPathList(FileIOPermissionAccess access, string path)
Adds access for the specified file or directory to the existing state of the permission.
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
static StreamWriter AppendText(string path)
Creates a T:System.IO.StreamWriter that appends UTF-8 encoded text to an existing file,...
Definition: File.cs:77
The file is a directory.
sealed override void Persist(string name, AccessControlSections includeSections)
Saves the specified sections of the security descriptor associated with this T:System....
static void WriteAllLines(string path, IEnumerable< string > contents, Encoding encoding)
Creates a new file by using the specified encoding, writes a collection of strings to the file,...
Definition: File.cs:1567
static void WriteAllLines(string path, IEnumerable< string > contents)
Creates a new file, writes a collection of strings to the file, and then closes the file.
Definition: File.cs:1532
Provides a T:System.IO.Stream for a file, supporting both synchronous and asynchronous read and write...
Definition: FileStream.cs:15
static FileStream Open(string path, FileMode mode)
Opens a T:System.IO.FileStream on the specified path with read/write access.
Definition: File.cs:516
static void Copy(string sourceFileName, string destFileName, bool overwrite)
Copies an existing file to a new file. Overwriting a file of the same name is allowed.
Definition: File.cs:144
Represents a writer that can write a sequential series of characters. This class is abstract.
Definition: TextWriter.cs:15
static void Copy(string sourceFileName, string destFileName)
Copies an existing file to a new file. Overwriting a file of the same name is not allowed.
Definition: File.cs:103
static void WriteAllText(string path, string contents)
Creates a new file, writes the specified string to the file, and then closes the file....
Definition: File.cs:1115
static void AppendAllText(string path, string contents)
Opens a file, appends the specified string to the file, and then closes the file. If the file does no...
Definition: File.cs:1615
Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks,...
Definition: Marshal.cs:15
static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share)
Opens a T:System.IO.FileStream on the specified path, having the specified mode with read,...
Definition: File.cs:571
Provides access to information on a drive.
Definition: DriveInfo.cs:13
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
The exception that is thrown when one of the arguments provided to a method is not valid.
void Demand()
Forces a T:System.Security.SecurityException at run time if all callers higher in the call stack have...
static void AppendAllLines(string path, IEnumerable< string > contents, Encoding encoding)
Appends lines to a file by using a specified encoding, and then closes the file. If the specified fil...
Definition: File.cs:1724
override void Write(char value)
Writes a character to the stream.
FileAccess
Defines constants for read, write, or read/write access to a file.
Definition: FileAccess.cs:9
static FileAttributes GetAttributes(string path)
Gets the T:System.IO.FileAttributes of the file on the path.
Definition: File.cs:884
static DateTime GetLastWriteTimeUtc(string path)
Returns the date and time, in coordinated universal time (UTC), that the specified file or directory ...
Definition: File.cs:849
virtual void WriteLine()
Writes a line terminator to the text string or stream.
Definition: TextWriter.cs:778
static void WriteAllLines(string path, string[] contents, Encoding encoding)
Creates a new file, writes the specified string array to the file by using the specified encoding,...
Definition: File.cs:1494
static Encoding UTF8
Gets an encoding for the UTF-8 format.
Definition: Encoding.cs:1023
string DriveFormat
Gets the name of the file system, such as NTFS or FAT32.
Definition: DriveInfo.cs:40
Provides static methods for the creation, copying, deletion, moving, and opening of a single file,...
Definition: File.cs:14
static void SetLastWriteTime(string path, DateTime lastWriteTime)
Sets the date and time that the specified file was last written to.
Definition: File.cs:786
static void Move(string sourceFileName, string destFileName)
Moves a specified file to a new location, providing the option to specify a new file name.
Definition: File.cs:1760
static IEnumerable< string > ReadLines(string path)
Reads the lines of a file.
Definition: File.cs:1398
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 void Delete(string path)
Deletes the specified file.
Definition: File.cs:324
static StreamReader OpenText(string path)
Opens an existing UTF-8 encoded text file for reading.
Definition: File.cs:35
static string [] ReadAllLines(string path)
Opens a text file, reads all lines of the file, and then closes the file.
Definition: File.cs:1319
The exception that is thrown when an invoked method is not supported, or when there is an attempt to ...
Controls the ability to access files and folders. This class cannot be inherited.
static FileStream Create(string path, int bufferSize, FileOptions options)
Creates or overwrites the specified file, specifying a buffer size and a T:System....
Definition: File.cs:277
static int GetLastWin32Error()
Returns the error code returned by the last unmanaged function that was called using platform invoke ...
DateTime ToUniversalTime()
Converts the value of the current T:System.DateTime object to Coordinated Universal Time (UTC).
Definition: DateTime.cs:1569
static string ReadAllText(string path)
Opens a text file, reads all lines of the file, and then closes the file.
Definition: File.cs:1026
static void SetLastAccessTime(string path, DateTime lastAccessTime)
Sets the date and time the specified file was last accessed.
Definition: File.cs:689
static byte [] ReadAllBytes(string path)
Opens a binary file, reads the contents of the file into a byte array, and then closes the file.
Definition: File.cs:1203
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
static FileStream Open(string path, FileMode mode, FileAccess access)
Opens a T:System.IO.FileStream on the specified path, with the specified mode and access.
Definition: File.cs:543
AccessControlSections
Specifies which sections of a security descriptor to save or load.
The P:System.Uri.LocalPath data.
The exception that is thrown when a security error is detected.
Performs operations on T:System.String instances that contain file or directory path information....
Definition: Path.cs:13
static DateTime GetLastAccessTime(string path)
Returns the date and time the specified file or directory was last accessed.
Definition: File.cs:735
static void WriteAllBytes(string path, byte[] bytes)
Creates a new file, writes the specified byte array to the file, and then closes the file....
Definition: File.cs:1258
FileShare
Contains constants for controlling the kind of access other T:System.IO.FileStream objects can have t...
Definition: FileShare.cs:9
static void AppendAllLines(string path, IEnumerable< string > contents)
Appends lines to a file, and then closes the file. If the specified file does not exist,...
Definition: File.cs:1688