-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
File.cs
1048 lines (896 loc) · 42.8 KB
/
File.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Buffers;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.ExceptionServices;
using System.Runtime.Versioning;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Win32.SafeHandles;
#if MS_IO_REDIST
using System;
using System.IO;
namespace Microsoft.IO
#else
namespace System.IO
#endif
{
// Class for creating FileStream objects, and some basic file management
// routines such as Delete, etc.
public static partial class File
{
// Don't use Array.MaxLength. MS.IO.Redist targets .NET Framework.
private const int MaxByteArrayLength = 0x7FFFFFC7;
private static Encoding? s_UTF8NoBOM;
internal const int DefaultBufferSize = 4096;
public static StreamReader OpenText(string path)
{
if (path == null)
throw new ArgumentNullException(nameof(path));
return new StreamReader(path);
}
public static StreamWriter CreateText(string path)
{
if (path == null)
throw new ArgumentNullException(nameof(path));
return new StreamWriter(path, append: false);
}
public static StreamWriter AppendText(string path)
{
if (path == null)
throw new ArgumentNullException(nameof(path));
return new StreamWriter(path, append: true);
}
/// <summary>
/// Copies an existing file to a new file.
/// An exception is raised if the destination file already exists.
/// </summary>
public static void Copy(string sourceFileName, string destFileName)
=> Copy(sourceFileName, destFileName, overwrite: false);
/// <summary>
/// Copies an existing file to a new file.
/// If <paramref name="overwrite"/> is false, an exception will be
/// raised if the destination exists. Otherwise it will be overwritten.
/// </summary>
public static void Copy(string sourceFileName, string destFileName, bool overwrite)
{
if (sourceFileName == null)
throw new ArgumentNullException(nameof(sourceFileName), SR.ArgumentNull_FileName);
if (destFileName == null)
throw new ArgumentNullException(nameof(destFileName), SR.ArgumentNull_FileName);
if (sourceFileName.Length == 0)
throw new ArgumentException(SR.Argument_EmptyFileName, nameof(sourceFileName));
if (destFileName.Length == 0)
throw new ArgumentException(SR.Argument_EmptyFileName, nameof(destFileName));
FileSystem.CopyFile(Path.GetFullPath(sourceFileName), Path.GetFullPath(destFileName), overwrite);
}
// Creates a file in a particular path. If the file exists, it is replaced.
// The file is opened with ReadWrite access and cannot be opened by another
// application until it has been closed. An IOException is thrown if the
// directory specified doesn't exist.
public static FileStream Create(string path)
{
return Create(path, DefaultBufferSize);
}
// Creates a file in a particular path. If the file exists, it is replaced.
// The file is opened with ReadWrite access and cannot be opened by another
// application until it has been closed. An IOException is thrown if the
// directory specified doesn't exist.
public static FileStream Create(string path, int bufferSize)
=> new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None, bufferSize);
public static FileStream Create(string path, int bufferSize, FileOptions options)
=> new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None, bufferSize, options);
// Deletes a file. The file specified by the designated path is deleted.
// If the file does not exist, Delete succeeds without throwing
// an exception.
//
// On Windows, Delete will fail for a file that is open for normal I/O
// or a file that is memory mapped.
public static void Delete(string path)
{
if (path == null)
throw new ArgumentNullException(nameof(path));
FileSystem.DeleteFile(Path.GetFullPath(path));
}
// Tests whether a file exists. The result is true if the file
// given by the specified path exists; otherwise, the result is
// false. Note that if path describes a directory,
// Exists will return true.
public static bool Exists([NotNullWhen(true)] string? path)
{
try
{
if (path == null)
return false;
if (path.Length == 0)
return false;
path = Path.GetFullPath(path);
// After normalizing, check whether path ends in directory separator.
// Otherwise, FillAttributeInfo removes it and we may return a false positive.
// GetFullPath should never return null
Debug.Assert(path != null, "File.Exists: GetFullPath returned null");
if (path.Length > 0 && PathInternal.IsDirectorySeparator(path[path.Length - 1]))
{
return false;
}
return FileSystem.FileExists(path);
}
catch (ArgumentException) { }
catch (IOException) { }
catch (UnauthorizedAccessException) { }
return false;
}
public static FileStream Open(string path, FileMode mode)
{
return Open(path, mode, (mode == FileMode.Append ? FileAccess.Write : FileAccess.ReadWrite), FileShare.None);
}
public static FileStream Open(string path, FileMode mode, FileAccess access)
{
return Open(path, mode, access, FileShare.None);
}
public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share)
{
return new FileStream(path, mode, access, share);
}
internal static DateTimeOffset GetUtcDateTimeOffset(DateTime dateTime)
{
// File and Directory UTC APIs treat a DateTimeKind.Unspecified as UTC whereas
// ToUniversalTime treats this as local.
if (dateTime.Kind == DateTimeKind.Unspecified)
{
return DateTime.SpecifyKind(dateTime, DateTimeKind.Utc);
}
return dateTime.ToUniversalTime();
}
public static void SetCreationTime(string path, DateTime creationTime)
{
string fullPath = Path.GetFullPath(path);
FileSystem.SetCreationTime(fullPath, creationTime, asDirectory: false);
}
public static void SetCreationTimeUtc(string path, DateTime creationTimeUtc)
{
string fullPath = Path.GetFullPath(path);
FileSystem.SetCreationTime(fullPath, GetUtcDateTimeOffset(creationTimeUtc), asDirectory: false);
}
public static DateTime GetCreationTime(string path)
{
string fullPath = Path.GetFullPath(path);
return FileSystem.GetCreationTime(fullPath).LocalDateTime;
}
public static DateTime GetCreationTimeUtc(string path)
{
string fullPath = Path.GetFullPath(path);
return FileSystem.GetCreationTime(fullPath).UtcDateTime;
}
public static void SetLastAccessTime(string path, DateTime lastAccessTime)
{
string fullPath = Path.GetFullPath(path);
FileSystem.SetLastAccessTime(fullPath, lastAccessTime, asDirectory: false);
}
public static void SetLastAccessTimeUtc(string path, DateTime lastAccessTimeUtc)
{
string fullPath = Path.GetFullPath(path);
FileSystem.SetLastAccessTime(fullPath, GetUtcDateTimeOffset(lastAccessTimeUtc), asDirectory: false);
}
public static DateTime GetLastAccessTime(string path)
{
string fullPath = Path.GetFullPath(path);
return FileSystem.GetLastAccessTime(fullPath).LocalDateTime;
}
public static DateTime GetLastAccessTimeUtc(string path)
{
string fullPath = Path.GetFullPath(path);
return FileSystem.GetLastAccessTime(fullPath).UtcDateTime;
}
public static void SetLastWriteTime(string path, DateTime lastWriteTime)
{
string fullPath = Path.GetFullPath(path);
FileSystem.SetLastWriteTime(fullPath, lastWriteTime, asDirectory: false);
}
public static void SetLastWriteTimeUtc(string path, DateTime lastWriteTimeUtc)
{
string fullPath = Path.GetFullPath(path);
FileSystem.SetLastWriteTime(fullPath, GetUtcDateTimeOffset(lastWriteTimeUtc), asDirectory: false);
}
public static DateTime GetLastWriteTime(string path)
{
string fullPath = Path.GetFullPath(path);
return FileSystem.GetLastWriteTime(fullPath).LocalDateTime;
}
public static DateTime GetLastWriteTimeUtc(string path)
{
string fullPath = Path.GetFullPath(path);
return FileSystem.GetLastWriteTime(fullPath).UtcDateTime;
}
public static FileAttributes GetAttributes(string path)
{
string fullPath = Path.GetFullPath(path);
return FileSystem.GetAttributes(fullPath);
}
public static void SetAttributes(string path, FileAttributes fileAttributes)
{
string fullPath = Path.GetFullPath(path);
FileSystem.SetAttributes(fullPath, fileAttributes);
}
public static FileStream OpenRead(string path)
{
return new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
}
public static FileStream OpenWrite(string path)
{
return new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
}
public static string ReadAllText(string path)
{
if (path == null)
throw new ArgumentNullException(nameof(path));
if (path.Length == 0)
throw new ArgumentException(SR.Argument_EmptyPath, nameof(path));
return InternalReadAllText(path, Encoding.UTF8);
}
public static string ReadAllText(string path, Encoding encoding)
{
if (path == null)
throw new ArgumentNullException(nameof(path));
if (encoding == null)
throw new ArgumentNullException(nameof(encoding));
if (path.Length == 0)
throw new ArgumentException(SR.Argument_EmptyPath, nameof(path));
return InternalReadAllText(path, encoding);
}
private static string InternalReadAllText(string path, Encoding encoding)
{
Debug.Assert(path != null);
Debug.Assert(encoding != null);
Debug.Assert(path.Length > 0);
using (StreamReader sr = new StreamReader(path, encoding, detectEncodingFromByteOrderMarks: true))
return sr.ReadToEnd();
}
public static void WriteAllText(string path, string? contents)
{
if (path == null)
throw new ArgumentNullException(nameof(path));
if (path.Length == 0)
throw new ArgumentException(SR.Argument_EmptyPath, nameof(path));
using (StreamWriter sw = new StreamWriter(path))
{
sw.Write(contents);
}
}
public static void WriteAllText(string path, string? contents, Encoding encoding)
{
if (path == null)
throw new ArgumentNullException(nameof(path));
if (encoding == null)
throw new ArgumentNullException(nameof(encoding));
if (path.Length == 0)
throw new ArgumentException(SR.Argument_EmptyPath, nameof(path));
using (StreamWriter sw = new StreamWriter(path, false, encoding))
{
sw.Write(contents);
}
}
public static byte[] ReadAllBytes(string path)
{
// bufferSize == 1 used to avoid unnecessary buffer in FileStream
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 1, FileOptions.SequentialScan))
{
long fileLength = fs.Length;
if (fileLength > int.MaxValue)
{
throw new IOException(SR.IO_FileTooLong2GB);
}
else if (fileLength == 0)
{
#if !MS_IO_REDIST
// Some file systems (e.g. procfs on Linux) return 0 for length even when there's content.
// Thus we need to assume 0 doesn't mean empty.
return ReadAllBytesUnknownLength(fs);
#endif
}
int index = 0;
int count = (int)fileLength;
byte[] bytes = new byte[count];
while (count > 0)
{
int n = fs.Read(bytes, index, count);
if (n == 0)
{
ThrowHelper.ThrowEndOfFileException();
}
index += n;
count -= n;
}
return bytes;
}
}
public static void WriteAllBytes(string path, byte[] bytes)
{
if (path == null)
throw new ArgumentNullException(nameof(path), SR.ArgumentNull_Path);
if (path.Length == 0)
throw new ArgumentException(SR.Argument_EmptyPath, nameof(path));
if (bytes == null)
throw new ArgumentNullException(nameof(bytes));
#if MS_IO_REDIST
using FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read);
fs.Write(bytes, 0, bytes.Length);
#else
using SafeFileHandle sfh = OpenHandle(path, FileMode.Create, FileAccess.Write, FileShare.Read);
RandomAccess.WriteAtOffset(sfh, bytes, 0);
#endif
}
public static string[] ReadAllLines(string path)
{
if (path == null)
throw new ArgumentNullException(nameof(path));
if (path.Length == 0)
throw new ArgumentException(SR.Argument_EmptyPath, nameof(path));
return InternalReadAllLines(path, Encoding.UTF8);
}
public static string[] ReadAllLines(string path, Encoding encoding)
{
if (path == null)
throw new ArgumentNullException(nameof(path));
if (encoding == null)
throw new ArgumentNullException(nameof(encoding));
if (path.Length == 0)
throw new ArgumentException(SR.Argument_EmptyPath, nameof(path));
return InternalReadAllLines(path, encoding);
}
private static string[] InternalReadAllLines(string path, Encoding encoding)
{
Debug.Assert(path != null);
Debug.Assert(encoding != null);
Debug.Assert(path.Length != 0);
string? line;
List<string> lines = new List<string>();
using (StreamReader sr = new StreamReader(path, encoding))
while ((line = sr.ReadLine()) != null)
lines.Add(line);
return lines.ToArray();
}
public static IEnumerable<string> ReadLines(string path)
{
if (path == null)
throw new ArgumentNullException(nameof(path));
if (path.Length == 0)
throw new ArgumentException(SR.Argument_EmptyPath, nameof(path));
return ReadLinesIterator.CreateIterator(path, Encoding.UTF8);
}
public static IEnumerable<string> ReadLines(string path, Encoding encoding)
{
if (path == null)
throw new ArgumentNullException(nameof(path));
if (encoding == null)
throw new ArgumentNullException(nameof(encoding));
if (path.Length == 0)
throw new ArgumentException(SR.Argument_EmptyPath, nameof(path));
return ReadLinesIterator.CreateIterator(path, encoding);
}
public static void WriteAllLines(string path, string[] contents)
{
WriteAllLines(path, (IEnumerable<string>)contents);
}
public static void WriteAllLines(string path, IEnumerable<string> contents)
{
if (path == null)
throw new ArgumentNullException(nameof(path));
if (contents == null)
throw new ArgumentNullException(nameof(contents));
if (path.Length == 0)
throw new ArgumentException(SR.Argument_EmptyPath, nameof(path));
InternalWriteAllLines(new StreamWriter(path), contents);
}
public static void WriteAllLines(string path, string[] contents, Encoding encoding)
{
WriteAllLines(path, (IEnumerable<string>)contents, encoding);
}
public static void WriteAllLines(string path, IEnumerable<string> contents, Encoding encoding)
{
if (path == null)
throw new ArgumentNullException(nameof(path));
if (contents == null)
throw new ArgumentNullException(nameof(contents));
if (encoding == null)
throw new ArgumentNullException(nameof(encoding));
if (path.Length == 0)
throw new ArgumentException(SR.Argument_EmptyPath, nameof(path));
InternalWriteAllLines(new StreamWriter(path, false, encoding), contents);
}
private static void InternalWriteAllLines(TextWriter writer, IEnumerable<string> contents)
{
Debug.Assert(writer != null);
Debug.Assert(contents != null);
using (writer)
{
foreach (string line in contents)
{
writer.WriteLine(line);
}
}
}
public static void AppendAllText(string path, string? contents)
{
if (path == null)
throw new ArgumentNullException(nameof(path));
if (path.Length == 0)
throw new ArgumentException(SR.Argument_EmptyPath, nameof(path));
using (StreamWriter sw = new StreamWriter(path, append: true))
{
sw.Write(contents);
}
}
public static void AppendAllText(string path, string? contents, Encoding encoding)
{
if (path == null)
throw new ArgumentNullException(nameof(path));
if (encoding == null)
throw new ArgumentNullException(nameof(encoding));
if (path.Length == 0)
throw new ArgumentException(SR.Argument_EmptyPath, nameof(path));
using (StreamWriter sw = new StreamWriter(path, true, encoding))
{
sw.Write(contents);
}
}
public static void AppendAllLines(string path, IEnumerable<string> contents)
{
if (path == null)
throw new ArgumentNullException(nameof(path));
if (contents == null)
throw new ArgumentNullException(nameof(contents));
if (path.Length == 0)
throw new ArgumentException(SR.Argument_EmptyPath, nameof(path));
InternalWriteAllLines(new StreamWriter(path, append: true), contents);
}
public static void AppendAllLines(string path, IEnumerable<string> contents, Encoding encoding)
{
if (path == null)
throw new ArgumentNullException(nameof(path));
if (contents == null)
throw new ArgumentNullException(nameof(contents));
if (encoding == null)
throw new ArgumentNullException(nameof(encoding));
if (path.Length == 0)
throw new ArgumentException(SR.Argument_EmptyPath, nameof(path));
InternalWriteAllLines(new StreamWriter(path, true, encoding), contents);
}
public static void Replace(string sourceFileName, string destinationFileName, string? destinationBackupFileName)
{
Replace(sourceFileName, destinationFileName, destinationBackupFileName, ignoreMetadataErrors: false);
}
public static void Replace(string sourceFileName, string destinationFileName, string? destinationBackupFileName, bool ignoreMetadataErrors)
{
if (sourceFileName == null)
throw new ArgumentNullException(nameof(sourceFileName));
if (destinationFileName == null)
throw new ArgumentNullException(nameof(destinationFileName));
FileSystem.ReplaceFile(
Path.GetFullPath(sourceFileName),
Path.GetFullPath(destinationFileName),
destinationBackupFileName != null ? Path.GetFullPath(destinationBackupFileName) : null,
ignoreMetadataErrors);
}
// Moves a specified file to a new location and potentially a new file name.
// This method does work across volumes.
//
// The caller must have certain FileIOPermissions. The caller must
// have Read and Write permission to
// sourceFileName and Write
// permissions to destFileName.
//
public static void Move(string sourceFileName, string destFileName)
{
Move(sourceFileName, destFileName, false);
}
public static void Move(string sourceFileName, string destFileName, bool overwrite)
{
if (sourceFileName == null)
throw new ArgumentNullException(nameof(sourceFileName), SR.ArgumentNull_FileName);
if (destFileName == null)
throw new ArgumentNullException(nameof(destFileName), SR.ArgumentNull_FileName);
if (sourceFileName.Length == 0)
throw new ArgumentException(SR.Argument_EmptyFileName, nameof(sourceFileName));
if (destFileName.Length == 0)
throw new ArgumentException(SR.Argument_EmptyFileName, nameof(destFileName));
string fullSourceFileName = Path.GetFullPath(sourceFileName);
string fullDestFileName = Path.GetFullPath(destFileName);
if (!FileSystem.FileExists(fullSourceFileName))
{
throw new FileNotFoundException(SR.Format(SR.IO_FileNotFound_FileName, fullSourceFileName), fullSourceFileName);
}
FileSystem.MoveFile(fullSourceFileName, fullDestFileName, overwrite);
}
[SupportedOSPlatform("windows")]
public static void Encrypt(string path)
{
FileSystem.Encrypt(path ?? throw new ArgumentNullException(nameof(path)));
}
[SupportedOSPlatform("windows")]
public static void Decrypt(string path)
{
FileSystem.Decrypt(path ?? throw new ArgumentNullException(nameof(path)));
}
// UTF-8 without BOM and with error detection. Same as the default encoding for StreamWriter.
private static Encoding UTF8NoBOM => s_UTF8NoBOM ?? (s_UTF8NoBOM = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true));
// If we use the path-taking constructors we will not have FileOptions.Asynchronous set and
// we will have asynchronous file access faked by the thread pool. We want the real thing.
private static StreamReader AsyncStreamReader(string path, Encoding encoding)
{
FileStream stream = new FileStream(
path, FileMode.Open, FileAccess.Read, FileShare.Read, DefaultBufferSize,
FileOptions.Asynchronous | FileOptions.SequentialScan);
return new StreamReader(stream, encoding, detectEncodingFromByteOrderMarks: true);
}
private static StreamWriter AsyncStreamWriter(string path, Encoding encoding, bool append)
{
FileStream stream = new FileStream(
path, append ? FileMode.Append : FileMode.Create, FileAccess.Write, FileShare.Read, DefaultBufferSize,
FileOptions.Asynchronous | FileOptions.SequentialScan);
return new StreamWriter(stream, encoding);
}
public static Task<string> ReadAllTextAsync(string path, CancellationToken cancellationToken = default(CancellationToken))
=> ReadAllTextAsync(path, Encoding.UTF8, cancellationToken);
public static Task<string> ReadAllTextAsync(string path, Encoding encoding, CancellationToken cancellationToken = default(CancellationToken))
{
if (path == null)
throw new ArgumentNullException(nameof(path));
if (encoding == null)
throw new ArgumentNullException(nameof(encoding));
if (path.Length == 0)
throw new ArgumentException(SR.Argument_EmptyPath, nameof(path));
return cancellationToken.IsCancellationRequested
? Task.FromCanceled<string>(cancellationToken)
: InternalReadAllTextAsync(path, encoding, cancellationToken);
}
private static async Task<string> InternalReadAllTextAsync(string path, Encoding encoding, CancellationToken cancellationToken)
{
Debug.Assert(!string.IsNullOrEmpty(path));
Debug.Assert(encoding != null);
char[]? buffer = null;
StreamReader sr = AsyncStreamReader(path, encoding);
try
{
cancellationToken.ThrowIfCancellationRequested();
buffer = ArrayPool<char>.Shared.Rent(sr.CurrentEncoding.GetMaxCharCount(DefaultBufferSize));
StringBuilder sb = new StringBuilder();
while (true)
{
#if MS_IO_REDIST
int read = await sr.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false);
#else
int read = await sr.ReadAsync(new Memory<char>(buffer), cancellationToken).ConfigureAwait(false);
#endif
if (read == 0)
{
return sb.ToString();
}
sb.Append(buffer, 0, read);
}
}
finally
{
sr.Dispose();
if (buffer != null)
{
ArrayPool<char>.Shared.Return(buffer);
}
}
}
public static Task WriteAllTextAsync(string path, string? contents, CancellationToken cancellationToken = default(CancellationToken))
=> WriteAllTextAsync(path, contents, UTF8NoBOM, cancellationToken);
public static Task WriteAllTextAsync(string path, string? contents, Encoding encoding, CancellationToken cancellationToken = default(CancellationToken))
{
if (path == null)
throw new ArgumentNullException(nameof(path));
if (encoding == null)
throw new ArgumentNullException(nameof(encoding));
if (path.Length == 0)
throw new ArgumentException(SR.Argument_EmptyPath, nameof(path));
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled(cancellationToken);
}
if (string.IsNullOrEmpty(contents))
{
new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read).Dispose();
return Task.CompletedTask;
}
return InternalWriteAllTextAsync(AsyncStreamWriter(path, encoding, append: false), contents, cancellationToken);
}
public static Task<byte[]> ReadAllBytesAsync(string path, CancellationToken cancellationToken = default(CancellationToken))
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled<byte[]>(cancellationToken);
}
var fs = new FileStream(
path, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 1, // bufferSize == 1 used to avoid unnecessary buffer in FileStream
FileOptions.Asynchronous | FileOptions.SequentialScan);
bool returningInternalTask = false;
try
{
long fileLength = fs.Length;
if (fileLength > int.MaxValue)
{
var e = new IOException(SR.IO_FileTooLong2GB);
#if !MS_IO_REDIST
ExceptionDispatchInfo.SetCurrentStackTrace(e);
#endif
return Task.FromException<byte[]>(e);
}
returningInternalTask = true;
return fileLength > 0 ?
InternalReadAllBytesAsync(fs, (int)fileLength, cancellationToken) :
InternalReadAllBytesUnknownLengthAsync(fs, cancellationToken);
}
finally
{
if (!returningInternalTask)
{
fs.Dispose();
}
}
}
private static async Task<byte[]> InternalReadAllBytesAsync(FileStream fs, int count, CancellationToken cancellationToken)
{
using (fs)
{
int index = 0;
byte[] bytes = new byte[count];
do
{
#if MS_IO_REDIST
int n = await fs.ReadAsync(bytes, index, count - index, cancellationToken).ConfigureAwait(false);
#else
int n = await fs.ReadAsync(new Memory<byte>(bytes, index, count - index), cancellationToken).ConfigureAwait(false);
#endif
if (n == 0)
{
ThrowHelper.ThrowEndOfFileException();
}
index += n;
} while (index < count);
return bytes;
}
}
private static async Task<byte[]> InternalReadAllBytesUnknownLengthAsync(FileStream fs, CancellationToken cancellationToken)
{
byte[] rentedArray = ArrayPool<byte>.Shared.Rent(512);
try
{
int bytesRead = 0;
while (true)
{
if (bytesRead == rentedArray.Length)
{
uint newLength = (uint)rentedArray.Length * 2;
if (newLength > MaxByteArrayLength)
{
newLength = (uint)Math.Max(MaxByteArrayLength, rentedArray.Length + 1);
}
byte[] tmp = ArrayPool<byte>.Shared.Rent((int)newLength);
Buffer.BlockCopy(rentedArray, 0, tmp, 0, bytesRead);
byte[] toReturn = rentedArray;
rentedArray = tmp;
ArrayPool<byte>.Shared.Return(toReturn);
}
Debug.Assert(bytesRead < rentedArray.Length);
#if MS_IO_REDIST
int n = await fs.ReadAsync(rentedArray, bytesRead, rentedArray.Length - bytesRead, cancellationToken).ConfigureAwait(false);
#else
int n = await fs.ReadAsync(rentedArray.AsMemory(bytesRead), cancellationToken).ConfigureAwait(false);
#endif
if (n == 0)
{
return rentedArray.AsSpan(0, bytesRead).ToArray();
}
bytesRead += n;
}
}
finally
{
fs.Dispose();
ArrayPool<byte>.Shared.Return(rentedArray);
}
}
public static Task WriteAllBytesAsync(string path, byte[] bytes, CancellationToken cancellationToken = default(CancellationToken))
{
if (path == null)
throw new ArgumentNullException(nameof(path), SR.ArgumentNull_Path);
if (path.Length == 0)
throw new ArgumentException(SR.Argument_EmptyPath, nameof(path));
if (bytes == null)
throw new ArgumentNullException(nameof(bytes));
return cancellationToken.IsCancellationRequested
? Task.FromCanceled(cancellationToken)
: Core(path, bytes, cancellationToken);
static async Task Core(string path, byte[] bytes, CancellationToken cancellationToken)
{
#if MS_IO_REDIST
using FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read, 1, FileOptions.Asynchronous | FileOptions.SequentialScan);
await fs.WriteAsync(bytes, 0, bytes.Length, cancellationToken).ConfigureAwait(false);
#else
using SafeFileHandle sfh = OpenHandle(path, FileMode.Create, FileAccess.Write, FileShare.Read, FileOptions.Asynchronous | FileOptions.SequentialScan);
await RandomAccess.WriteAtOffsetAsync(sfh, bytes, 0, cancellationToken).ConfigureAwait(false);
#endif
}
}
public static Task<string[]> ReadAllLinesAsync(string path, CancellationToken cancellationToken = default(CancellationToken))
=> ReadAllLinesAsync(path, Encoding.UTF8, cancellationToken);
public static Task<string[]> ReadAllLinesAsync(string path, Encoding encoding, CancellationToken cancellationToken = default(CancellationToken))
{
if (path == null)
throw new ArgumentNullException(nameof(path));
if (encoding == null)
throw new ArgumentNullException(nameof(encoding));
if (path.Length == 0)
throw new ArgumentException(SR.Argument_EmptyPath, nameof(path));
return cancellationToken.IsCancellationRequested
? Task.FromCanceled<string[]>(cancellationToken)
: InternalReadAllLinesAsync(path, encoding, cancellationToken);
}
private static async Task<string[]> InternalReadAllLinesAsync(string path, Encoding encoding, CancellationToken cancellationToken)
{
Debug.Assert(!string.IsNullOrEmpty(path));
Debug.Assert(encoding != null);
using (StreamReader sr = AsyncStreamReader(path, encoding))
{
cancellationToken.ThrowIfCancellationRequested();
string? line;
List<string> lines = new List<string>();
while ((line = await sr.ReadLineAsync().ConfigureAwait(false)) != null)
{
lines.Add(line);
cancellationToken.ThrowIfCancellationRequested();
}
return lines.ToArray();
}
}
public static Task WriteAllLinesAsync(string path, IEnumerable<string> contents, CancellationToken cancellationToken = default(CancellationToken))
=> WriteAllLinesAsync(path, contents, UTF8NoBOM, cancellationToken);
public static Task WriteAllLinesAsync(string path, IEnumerable<string> contents, Encoding encoding, CancellationToken cancellationToken = default(CancellationToken))
{
if (path == null)
throw new ArgumentNullException(nameof(path));
if (contents == null)
throw new ArgumentNullException(nameof(contents));
if (encoding == null)
throw new ArgumentNullException(nameof(encoding));
if (path.Length == 0)
throw new ArgumentException(SR.Argument_EmptyPath, nameof(path));
return cancellationToken.IsCancellationRequested
? Task.FromCanceled(cancellationToken)
: InternalWriteAllLinesAsync(AsyncStreamWriter(path, encoding, append: false), contents, cancellationToken);
}
private static async Task InternalWriteAllLinesAsync(TextWriter writer, IEnumerable<string> contents, CancellationToken cancellationToken)
{
Debug.Assert(writer != null);
Debug.Assert(contents != null);
using (writer)
{
foreach (string line in contents)
{
cancellationToken.ThrowIfCancellationRequested();
await writer.WriteLineAsync(line).ConfigureAwait(false);
}
cancellationToken.ThrowIfCancellationRequested();
await writer.FlushAsync().ConfigureAwait(false);
}
}
private static async Task InternalWriteAllTextAsync(StreamWriter sw, string contents, CancellationToken cancellationToken)
{
#if MS_IO_REDIST
char[]? buffer = null;
try
{
buffer = ArrayPool<char>.Shared.Rent(DefaultBufferSize);
int count = contents.Length;
int index = 0;
while (index < count)
{
int batchSize = Math.Min(DefaultBufferSize, count - index);
contents.CopyTo(index, buffer, 0, batchSize);
await sw.WriteAsync(buffer, 0, batchSize).ConfigureAwait(false);
index += batchSize;
}
cancellationToken.ThrowIfCancellationRequested();
await sw.FlushAsync().ConfigureAwait(false);
}
finally
{
sw.Dispose();
if (buffer != null)
{
ArrayPool<char>.Shared.Return(buffer);
}
}
#else
using (sw)
{
await sw.WriteAsync(contents.AsMemory(), cancellationToken).ConfigureAwait(false);
await sw.FlushAsync().ConfigureAwait(false);
}
#endif
}
public static Task AppendAllTextAsync(string path, string? contents, CancellationToken cancellationToken = default(CancellationToken))
=> AppendAllTextAsync(path, contents, UTF8NoBOM, cancellationToken);
public static Task AppendAllTextAsync(string path, string? contents, Encoding encoding, CancellationToken cancellationToken = default(CancellationToken))
{
if (path == null)
throw new ArgumentNullException(nameof(path));
if (encoding == null)
throw new ArgumentNullException(nameof(encoding));
if (path.Length == 0)
throw new ArgumentException(SR.Argument_EmptyPath, nameof(path));
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled(cancellationToken);
}
if (string.IsNullOrEmpty(contents))
{
// Just to throw exception if there is a problem opening the file.
new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.Read).Dispose();
return Task.CompletedTask;
}
return InternalWriteAllTextAsync(AsyncStreamWriter(path, encoding, append: true), contents, cancellationToken);
}
public static Task AppendAllLinesAsync(string path, IEnumerable<string> contents, CancellationToken cancellationToken = default(CancellationToken))
=> AppendAllLinesAsync(path, contents, UTF8NoBOM, cancellationToken);
public static Task AppendAllLinesAsync(string path, IEnumerable<string> contents, Encoding encoding, CancellationToken cancellationToken = default(CancellationToken))
{
if (path == null)
throw new ArgumentNullException(nameof(path));
if (contents == null)
throw new ArgumentNullException(nameof(contents));
if (encoding == null)
throw new ArgumentNullException(nameof(encoding));
if (path.Length == 0)