Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
File.cs
Go to the documentation of this file.
7using System.Text;
11
12namespace System.IO;
13
14public static class File
15{
16 private static Encoding s_UTF8NoBOM;
17
19
20 public static StreamReader OpenText(string path)
21 {
22 if (path == null)
23 {
24 throw new ArgumentNullException("path");
25 }
26 return new StreamReader(path);
27 }
28
29 public static StreamWriter CreateText(string path)
30 {
31 if (path == null)
32 {
33 throw new ArgumentNullException("path");
34 }
35 return new StreamWriter(path, append: false);
36 }
37
38 public static StreamWriter AppendText(string path)
39 {
40 if (path == null)
41 {
42 throw new ArgumentNullException("path");
43 }
44 return new StreamWriter(path, append: true);
45 }
46
47 public static void Copy(string sourceFileName, string destFileName)
48 {
50 }
51
52 public static void Copy(string sourceFileName, string destFileName, bool overwrite)
53 {
54 if (sourceFileName == null)
55 {
56 throw new ArgumentNullException("sourceFileName", SR.ArgumentNull_FileName);
57 }
58 if (destFileName == null)
59 {
60 throw new ArgumentNullException("destFileName", SR.ArgumentNull_FileName);
61 }
62 if (sourceFileName.Length == 0)
63 {
64 throw new ArgumentException(SR.Argument_EmptyFileName, "sourceFileName");
65 }
66 if (destFileName.Length == 0)
67 {
68 throw new ArgumentException(SR.Argument_EmptyFileName, "destFileName");
69 }
71 }
72
73 public static FileStream Create(string path)
74 {
75 return Create(path, 4096);
76 }
77
78 public static FileStream Create(string path, int bufferSize)
79 {
80 return new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None, bufferSize);
81 }
82
83 public static FileStream Create(string path, int bufferSize, FileOptions options)
84 {
85 return new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None, bufferSize, options);
86 }
87
88 public static void Delete(string path)
89 {
90 if (path == null)
91 {
92 throw new ArgumentNullException("path");
93 }
95 }
96
97 public static bool Exists([NotNullWhen(true)] string? path)
98 {
99 try
100 {
101 if (path == null)
102 {
103 return false;
104 }
105 if (path.Length == 0)
106 {
107 return false;
108 }
110 if (path.Length > 0 && PathInternal.IsDirectorySeparator(path[path.Length - 1]))
111 {
112 return false;
113 }
114 return FileSystem.FileExists(path);
115 }
116 catch (ArgumentException)
117 {
118 }
119 catch (IOException)
120 {
121 }
123 {
124 }
125 return false;
126 }
127
128 public static FileStream Open(string path, FileMode mode)
129 {
130 return Open(path, mode, (mode == FileMode.Append) ? FileAccess.Write : FileAccess.ReadWrite, FileShare.None);
131 }
132
133 public static FileStream Open(string path, FileMode mode, FileAccess access)
134 {
135 return Open(path, mode, access, FileShare.None);
136 }
137
139 {
140 return new FileStream(path, mode, access, share);
141 }
142
144 {
145 if (dateTime.Kind == DateTimeKind.Unspecified)
146 {
148 }
149 return dateTime.ToUniversalTime();
150 }
151
152 public static void SetCreationTime(string path, DateTime creationTime)
153 {
154 string fullPath = Path.GetFullPath(path);
156 }
157
163
164 public static DateTime GetCreationTime(string path)
165 {
166 string fullPath = Path.GetFullPath(path);
167 return FileSystem.GetCreationTime(fullPath).LocalDateTime;
168 }
169
170 public static DateTime GetCreationTimeUtc(string path)
171 {
172 string fullPath = Path.GetFullPath(path);
173 return FileSystem.GetCreationTime(fullPath).UtcDateTime;
174 }
175
181
187
188 public static DateTime GetLastAccessTime(string path)
189 {
190 string fullPath = Path.GetFullPath(path);
191 return FileSystem.GetLastAccessTime(fullPath).LocalDateTime;
192 }
193
194 public static DateTime GetLastAccessTimeUtc(string path)
195 {
196 string fullPath = Path.GetFullPath(path);
197 return FileSystem.GetLastAccessTime(fullPath).UtcDateTime;
198 }
199
205
211
212 public static DateTime GetLastWriteTime(string path)
213 {
214 string fullPath = Path.GetFullPath(path);
215 return FileSystem.GetLastWriteTime(fullPath).LocalDateTime;
216 }
217
218 public static DateTime GetLastWriteTimeUtc(string path)
219 {
220 string fullPath = Path.GetFullPath(path);
221 return FileSystem.GetLastWriteTime(fullPath).UtcDateTime;
222 }
223
224 public static FileAttributes GetAttributes(string path)
225 {
226 string fullPath = Path.GetFullPath(path);
228 }
229
230 public static void SetAttributes(string path, FileAttributes fileAttributes)
231 {
232 string fullPath = Path.GetFullPath(path);
233 FileSystem.SetAttributes(fullPath, fileAttributes);
234 }
235
236 public static FileStream OpenRead(string path)
237 {
238 return new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
239 }
240
241 public static FileStream OpenWrite(string path)
242 {
243 return new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
244 }
245
246 public static string ReadAllText(string path)
247 {
248 if (path == null)
249 {
250 throw new ArgumentNullException("path");
251 }
252 if (path.Length == 0)
253 {
254 throw new ArgumentException(SR.Argument_EmptyPath, "path");
255 }
257 }
258
259 public static string ReadAllText(string path, Encoding encoding)
260 {
261 if (path == null)
262 {
263 throw new ArgumentNullException("path");
264 }
265 if (encoding == null)
266 {
267 throw new ArgumentNullException("encoding");
268 }
269 if (path.Length == 0)
270 {
271 throw new ArgumentException(SR.Argument_EmptyPath, "path");
272 }
273 return InternalReadAllText(path, encoding);
274 }
275
276 private static string InternalReadAllText(string path, Encoding encoding)
277 {
279 return streamReader.ReadToEnd();
280 }
281
282 public static void WriteAllText(string path, string? contents)
283 {
284 if (path == null)
285 {
286 throw new ArgumentNullException("path");
287 }
288 if (path.Length == 0)
289 {
290 throw new ArgumentException(SR.Argument_EmptyPath, "path");
291 }
293 streamWriter.Write(contents);
294 }
295
296 public static void WriteAllText(string path, string? contents, Encoding encoding)
297 {
298 if (path == null)
299 {
300 throw new ArgumentNullException("path");
301 }
302 if (encoding == null)
303 {
304 throw new ArgumentNullException("encoding");
305 }
306 if (path.Length == 0)
307 {
308 throw new ArgumentException(SR.Argument_EmptyPath, "path");
309 }
310 using StreamWriter streamWriter = new StreamWriter(path, append: false, encoding);
311 streamWriter.Write(contents);
312 }
313
314 public static byte[] ReadAllBytes(string path)
315 {
316 using FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 1, FileOptions.SequentialScan);
317 long length = fileStream.Length;
318 if (length > int.MaxValue)
319 {
321 }
322 if (length == 0L)
323 {
325 }
326 int num = 0;
327 int num2 = (int)length;
328 byte[] array = new byte[num2];
329 while (num2 > 0)
330 {
331 int num3 = fileStream.Read(array, num, num2);
332 if (num3 == 0)
333 {
335 }
336 num += num3;
337 num2 -= num3;
338 }
339 return array;
340 }
341
342 public static void WriteAllBytes(string path, byte[] bytes)
343 {
344 if (path == null)
345 {
346 throw new ArgumentNullException("path", SR.ArgumentNull_Path);
347 }
348 if (path.Length == 0)
349 {
350 throw new ArgumentException(SR.Argument_EmptyPath, "path");
351 }
352 if (bytes == null)
353 {
354 throw new ArgumentNullException("bytes");
355 }
356 using SafeFileHandle handle = OpenHandle(path, FileMode.Create, FileAccess.Write, FileShare.Read, FileOptions.None, 0L);
358 }
359
360 public static string[] ReadAllLines(string path)
361 {
362 if (path == null)
363 {
364 throw new ArgumentNullException("path");
365 }
366 if (path.Length == 0)
367 {
368 throw new ArgumentException(SR.Argument_EmptyPath, "path");
369 }
371 }
372
373 public static string[] ReadAllLines(string path, Encoding encoding)
374 {
375 if (path == null)
376 {
377 throw new ArgumentNullException("path");
378 }
379 if (encoding == null)
380 {
381 throw new ArgumentNullException("encoding");
382 }
383 if (path.Length == 0)
384 {
385 throw new ArgumentException(SR.Argument_EmptyPath, "path");
386 }
387 return InternalReadAllLines(path, encoding);
388 }
389
390 private static string[] InternalReadAllLines(string path, Encoding encoding)
391 {
393 using (StreamReader streamReader = new StreamReader(path, encoding))
394 {
395 string item;
396 while ((item = streamReader.ReadLine()) != null)
397 {
398 list.Add(item);
399 }
400 }
401 return list.ToArray();
402 }
403
404 public static IEnumerable<string> ReadLines(string path)
405 {
406 if (path == null)
407 {
408 throw new ArgumentNullException("path");
409 }
410 if (path.Length == 0)
411 {
412 throw new ArgumentException(SR.Argument_EmptyPath, "path");
413 }
415 }
416
417 public static IEnumerable<string> ReadLines(string path, Encoding encoding)
418 {
419 if (path == null)
420 {
421 throw new ArgumentNullException("path");
422 }
423 if (encoding == null)
424 {
425 throw new ArgumentNullException("encoding");
426 }
427 if (path.Length == 0)
428 {
429 throw new ArgumentException(SR.Argument_EmptyPath, "path");
430 }
431 return ReadLinesIterator.CreateIterator(path, encoding);
432 }
433
434 public static void WriteAllLines(string path, string[] contents)
435 {
437 }
438
439 public static void WriteAllLines(string path, IEnumerable<string> contents)
440 {
441 if (path == null)
442 {
443 throw new ArgumentNullException("path");
444 }
445 if (contents == null)
446 {
447 throw new ArgumentNullException("contents");
448 }
449 if (path.Length == 0)
450 {
451 throw new ArgumentException(SR.Argument_EmptyPath, "path");
452 }
454 }
455
456 public static void WriteAllLines(string path, string[] contents, Encoding encoding)
457 {
458 WriteAllLines(path, (IEnumerable<string>)contents, encoding);
459 }
460
461 public static void WriteAllLines(string path, IEnumerable<string> contents, Encoding encoding)
462 {
463 if (path == null)
464 {
465 throw new ArgumentNullException("path");
466 }
467 if (contents == null)
468 {
469 throw new ArgumentNullException("contents");
470 }
471 if (encoding == null)
472 {
473 throw new ArgumentNullException("encoding");
474 }
475 if (path.Length == 0)
476 {
477 throw new ArgumentException(SR.Argument_EmptyPath, "path");
478 }
479 InternalWriteAllLines(new StreamWriter(path, append: false, encoding), contents);
480 }
481
483 {
484 using (writer)
485 {
486 foreach (string content in contents)
487 {
488 writer.WriteLine(content);
489 }
490 }
491 }
492
493 public static void AppendAllText(string path, string? contents)
494 {
495 if (path == null)
496 {
497 throw new ArgumentNullException("path");
498 }
499 if (path.Length == 0)
500 {
501 throw new ArgumentException(SR.Argument_EmptyPath, "path");
502 }
504 streamWriter.Write(contents);
505 }
506
507 public static void AppendAllText(string path, string? contents, Encoding encoding)
508 {
509 if (path == null)
510 {
511 throw new ArgumentNullException("path");
512 }
513 if (encoding == null)
514 {
515 throw new ArgumentNullException("encoding");
516 }
517 if (path.Length == 0)
518 {
519 throw new ArgumentException(SR.Argument_EmptyPath, "path");
520 }
521 using StreamWriter streamWriter = new StreamWriter(path, append: true, encoding);
522 streamWriter.Write(contents);
523 }
524
525 public static void AppendAllLines(string path, IEnumerable<string> contents)
526 {
527 if (path == null)
528 {
529 throw new ArgumentNullException("path");
530 }
531 if (contents == null)
532 {
533 throw new ArgumentNullException("contents");
534 }
535 if (path.Length == 0)
536 {
537 throw new ArgumentException(SR.Argument_EmptyPath, "path");
538 }
539 InternalWriteAllLines(new StreamWriter(path, append: true), contents);
540 }
541
542 public static void AppendAllLines(string path, IEnumerable<string> contents, Encoding encoding)
543 {
544 if (path == null)
545 {
546 throw new ArgumentNullException("path");
547 }
548 if (contents == null)
549 {
550 throw new ArgumentNullException("contents");
551 }
552 if (encoding == null)
553 {
554 throw new ArgumentNullException("encoding");
555 }
556 if (path.Length == 0)
557 {
558 throw new ArgumentException(SR.Argument_EmptyPath, "path");
559 }
560 InternalWriteAllLines(new StreamWriter(path, append: true, encoding), contents);
561 }
562
567
569 {
570 if (sourceFileName == null)
571 {
572 throw new ArgumentNullException("sourceFileName");
573 }
574 if (destinationFileName == null)
575 {
576 throw new ArgumentNullException("destinationFileName");
577 }
579 }
580
581 public static void Move(string sourceFileName, string destFileName)
582 {
584 }
585
586 public static void Move(string sourceFileName, string destFileName, bool overwrite)
587 {
588 if (sourceFileName == null)
589 {
590 throw new ArgumentNullException("sourceFileName", SR.ArgumentNull_FileName);
591 }
592 if (destFileName == null)
593 {
594 throw new ArgumentNullException("destFileName", SR.ArgumentNull_FileName);
595 }
596 if (sourceFileName.Length == 0)
597 {
598 throw new ArgumentException(SR.Argument_EmptyFileName, "sourceFileName");
599 }
600 if (destFileName.Length == 0)
601 {
602 throw new ArgumentException(SR.Argument_EmptyFileName, "destFileName");
603 }
607 {
609 }
611 }
612
613 [SupportedOSPlatform("windows")]
614 public static void Encrypt(string path)
615 {
616 FileSystem.Encrypt(path ?? throw new ArgumentNullException("path"));
617 }
618
619 [SupportedOSPlatform("windows")]
620 public static void Decrypt(string path)
621 {
622 FileSystem.Decrypt(path ?? throw new ArgumentNullException("path"));
623 }
624
625 private static StreamReader AsyncStreamReader(string path, Encoding encoding)
626 {
627 FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.Asynchronous | FileOptions.SequentialScan);
628 return new StreamReader(stream, encoding, detectEncodingFromByteOrderMarks: true);
629 }
630
631 private static StreamWriter AsyncStreamWriter(string path, Encoding encoding, bool append)
632 {
633 FileStream stream = new FileStream(path, append ? FileMode.Append : FileMode.Create, FileAccess.Write, FileShare.Read, 4096, FileOptions.Asynchronous | FileOptions.SequentialScan);
634 return new StreamWriter(stream, encoding);
635 }
636
641
643 {
644 if (path == null)
645 {
646 throw new ArgumentNullException("path");
647 }
648 if (encoding == null)
649 {
650 throw new ArgumentNullException("encoding");
651 }
652 if (path.Length == 0)
653 {
654 throw new ArgumentException(SR.Argument_EmptyPath, "path");
655 }
656 if (!cancellationToken.IsCancellationRequested)
657 {
659 }
660 return Task.FromCanceled<string>(cancellationToken);
661 }
662
664 {
665 char[] buffer = null;
667 try
668 {
669 cancellationToken.ThrowIfCancellationRequested();
670 buffer = ArrayPool<char>.Shared.Rent(sr.CurrentEncoding.GetMaxCharCount(4096));
671 StringBuilder sb = new StringBuilder();
672 while (true)
673 {
674 int num = await sr.ReadAsync(new Memory<char>(buffer), cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
675 if (num == 0)
676 {
677 break;
678 }
679 sb.Append(buffer, 0, num);
680 }
681 return sb.ToString();
682 }
683 finally
684 {
685 sr.Dispose();
686 if (buffer != null)
687 {
688 ArrayPool<char>.Shared.Return(buffer);
689 }
690 }
691 }
692
693 public static Task WriteAllTextAsync(string path, string? contents, CancellationToken cancellationToken = default(CancellationToken))
694 {
696 }
697
698 public static Task WriteAllTextAsync(string path, string? contents, Encoding encoding, CancellationToken cancellationToken = default(CancellationToken))
699 {
700 if (path == null)
701 {
702 throw new ArgumentNullException("path");
703 }
704 if (encoding == null)
705 {
706 throw new ArgumentNullException("encoding");
707 }
708 if (path.Length == 0)
709 {
710 throw new ArgumentException(SR.Argument_EmptyPath, "path");
711 }
712 if (cancellationToken.IsCancellationRequested)
713 {
715 }
716 if (string.IsNullOrEmpty(contents))
717 {
718 new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read).Dispose();
719 return Task.CompletedTask;
720 }
721 return InternalWriteAllTextAsync(AsyncStreamWriter(path, encoding, append: false), contents, cancellationToken);
722 }
723
725 {
726 if (cancellationToken.IsCancellationRequested)
727 {
728 return Task.FromCanceled<byte[]>(cancellationToken);
729 }
730 FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 1, FileOptions.Asynchronous | FileOptions.SequentialScan);
731 bool flag = false;
732 try
733 {
734 long length = fileStream.Length;
735 if (length > int.MaxValue)
736 {
739 return Task.FromException<byte[]>(ex);
740 }
741 flag = true;
743 }
744 finally
745 {
746 if (!flag)
747 {
748 fileStream.Dispose();
749 }
750 }
751 }
752
754 {
755 using (fs)
756 {
757 int index = 0;
758 byte[] bytes = new byte[count];
759 do
760 {
761 int num = await fs.ReadAsync(new Memory<byte>(bytes, index, count - index), cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
762 if (num == 0)
763 {
765 }
766 index += num;
767 }
768 while (index < count);
769 return bytes;
770 }
771 }
772
774 {
775 byte[] rentedArray = ArrayPool<byte>.Shared.Rent(512);
776 try
777 {
778 int bytesRead = 0;
779 while (true)
780 {
781 if (bytesRead == rentedArray.Length)
782 {
783 uint num = (uint)(rentedArray.Length * 2);
784 if (num > 2147483591)
785 {
786 num = (uint)Math.Max(2147483591, rentedArray.Length + 1);
787 }
788 byte[] array = ArrayPool<byte>.Shared.Rent((int)num);
790 byte[] array2 = rentedArray;
792 ArrayPool<byte>.Shared.Return(array2);
793 }
794 int num2 = await fs.ReadAsync(rentedArray.AsMemory(bytesRead), cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
795 if (num2 == 0)
796 {
797 break;
798 }
799 bytesRead += num2;
800 }
801 return rentedArray.AsSpan(0, bytesRead).ToArray();
802 }
803 finally
804 {
805 fs.Dispose();
806 ArrayPool<byte>.Shared.Return(rentedArray);
807 }
808 }
809
811 {
812 if (path == null)
813 {
814 throw new ArgumentNullException("path", SR.ArgumentNull_Path);
815 }
816 if (path.Length == 0)
817 {
818 throw new ArgumentException(SR.Argument_EmptyPath, "path");
819 }
820 if (bytes == null)
821 {
822 throw new ArgumentNullException("bytes");
823 }
824 if (!cancellationToken.IsCancellationRequested)
825 {
827 }
830 {
831 using SafeFileHandle sfh = OpenHandle(path, FileMode.Create, FileAccess.Write, FileShare.Read, FileOptions.Asynchronous | FileOptions.SequentialScan, 0L);
833 }
834 }
835
840
842 {
843 if (path == null)
844 {
845 throw new ArgumentNullException("path");
846 }
847 if (encoding == null)
848 {
849 throw new ArgumentNullException("encoding");
850 }
851 if (path.Length == 0)
852 {
853 throw new ArgumentException(SR.Argument_EmptyPath, "path");
854 }
855 if (!cancellationToken.IsCancellationRequested)
856 {
858 }
859 return Task.FromCanceled<string[]>(cancellationToken);
860 }
861
863 {
864 using StreamReader sr = AsyncStreamReader(path, encoding);
865 cancellationToken.ThrowIfCancellationRequested();
867 string item;
868 while ((item = await sr.ReadLineAsync().ConfigureAwait(continueOnCapturedContext: false)) != null)
869 {
870 lines.Add(item);
871 cancellationToken.ThrowIfCancellationRequested();
872 }
873 return lines.ToArray();
874 }
875
880
882 {
883 if (path == null)
884 {
885 throw new ArgumentNullException("path");
886 }
887 if (contents == null)
888 {
889 throw new ArgumentNullException("contents");
890 }
891 if (encoding == null)
892 {
893 throw new ArgumentNullException("encoding");
894 }
895 if (path.Length == 0)
896 {
897 throw new ArgumentException(SR.Argument_EmptyPath, "path");
898 }
899 if (!cancellationToken.IsCancellationRequested)
900 {
901 return InternalWriteAllLinesAsync(AsyncStreamWriter(path, encoding, append: false), contents, cancellationToken);
902 }
904 }
905
907 {
908 using (writer)
909 {
910 foreach (string content in contents)
911 {
912 cancellationToken.ThrowIfCancellationRequested();
913 await writer.WriteLineAsync(content).ConfigureAwait(continueOnCapturedContext: false);
914 }
915 cancellationToken.ThrowIfCancellationRequested();
916 await writer.FlushAsync().ConfigureAwait(continueOnCapturedContext: false);
917 }
918 }
919
921 {
922 using (sw)
923 {
924 await sw.WriteAsync(contents.AsMemory(), cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
925 await sw.FlushAsync().ConfigureAwait(continueOnCapturedContext: false);
926 }
927 }
928
929 public static Task AppendAllTextAsync(string path, string? contents, CancellationToken cancellationToken = default(CancellationToken))
930 {
932 }
933
934 public static Task AppendAllTextAsync(string path, string? contents, Encoding encoding, CancellationToken cancellationToken = default(CancellationToken))
935 {
936 if (path == null)
937 {
938 throw new ArgumentNullException("path");
939 }
940 if (encoding == null)
941 {
942 throw new ArgumentNullException("encoding");
943 }
944 if (path.Length == 0)
945 {
946 throw new ArgumentException(SR.Argument_EmptyPath, "path");
947 }
948 if (cancellationToken.IsCancellationRequested)
949 {
951 }
952 if (string.IsNullOrEmpty(contents))
953 {
954 new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.Read).Dispose();
955 return Task.CompletedTask;
956 }
957 return InternalWriteAllTextAsync(AsyncStreamWriter(path, encoding, append: true), contents, cancellationToken);
958 }
959
964
966 {
967 if (path == null)
968 {
969 throw new ArgumentNullException("path");
970 }
971 if (contents == null)
972 {
973 throw new ArgumentNullException("contents");
974 }
975 if (encoding == null)
976 {
977 throw new ArgumentNullException("encoding");
978 }
979 if (path.Length == 0)
980 {
981 throw new ArgumentException(SR.Argument_EmptyPath, "path");
982 }
983 if (!cancellationToken.IsCancellationRequested)
984 {
985 return InternalWriteAllLinesAsync(AsyncStreamWriter(path, encoding, append: true), contents, cancellationToken);
986 }
988 }
989
991 {
992 string fullPath = Path.GetFullPath(path);
993 FileSystem.VerifyValidPath(pathToTarget, "pathToTarget");
995 return new FileInfo(path, fullPath, null, isNormalized: true);
996 }
997
1003
1005 {
1006 return new FileStream(path, options);
1007 }
1008
1009 public static SafeFileHandle OpenHandle(string path, FileMode mode = FileMode.Open, FileAccess access = FileAccess.Read, FileShare share = FileShare.Read, FileOptions options = FileOptions.None, long preallocationSize = 0L)
1010 {
1011 FileStreamHelpers.ValidateArguments(path, mode, access, share, 0, options, preallocationSize);
1012 return SafeFileHandle.Open(Path.GetFullPath(path), mode, access, share, options, preallocationSize);
1013 }
1014
1016 {
1017 byte[] array = null;
1018 Span<byte> span = stackalloc byte[512];
1019 try
1020 {
1021 int num = 0;
1022 while (true)
1023 {
1024 if (num == span.Length)
1025 {
1026 uint num2 = (uint)(span.Length * 2);
1027 if (num2 > Array.MaxLength)
1028 {
1029 num2 = (uint)Math.Max(Array.MaxLength, span.Length + 1);
1030 }
1031 byte[] array2 = ArrayPool<byte>.Shared.Rent((int)num2);
1033 byte[] array3 = array;
1034 span = (array = array2);
1035 if (array3 != null)
1036 {
1037 ArrayPool<byte>.Shared.Return(array3);
1038 }
1039 }
1040 int num3 = fs.Read(span.Slice(num));
1041 if (num3 == 0)
1042 {
1043 break;
1044 }
1045 num += num3;
1046 }
1047 return span.Slice(0, num).ToArray();
1048 }
1049 finally
1050 {
1051 if (array != null)
1052 {
1053 ArrayPool<byte>.Shared.Return(array);
1054 }
1055 }
1056 }
1057}
static SafeFileHandle Open(string fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, long preallocationSize)
static int MaxLength
Definition Array.cs:471
static void BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count)
Definition Buffer.cs:102
void CopyTo(KeyValuePair< TKey, TValue >[] array, int index)
void Add(TKey key, TValue value)
override void Dispose(bool disposing)
static DateTimeOffset GetLastAccessTime(string fullPath)
static void DeleteFile(string fullPath)
static void Encrypt(string path)
static FileSystemInfo ResolveLinkTarget(string linkPath, bool returnFinalTarget, bool isDirectory)
static DateTimeOffset GetLastWriteTime(string fullPath)
static void MoveFile(string sourceFullPath, string destFullPath, bool overwrite)
static void CopyFile(string sourceFullPath, string destFullPath, bool overwrite)
static void SetCreationTime(string fullPath, DateTimeOffset time, bool asDirectory)
static bool FileExists(string fullPath)
Definition FileSystem.cs:26
static void Decrypt(string path)
static void SetLastWriteTime(string fullPath, DateTimeOffset time, bool asDirectory)
static DateTimeOffset GetCreationTime(string fullPath)
static void VerifyValidPath(string path, string argName)
Definition FileSystem.cs:10
static void SetAttributes(string fullPath, FileAttributes attributes)
static void ReplaceFile(string sourceFullPath, string destFullPath, string destBackupFullPath, bool ignoreMetadataErrors)
static void CreateSymbolicLink(string path, string pathToTarget, bool isDirectory)
static void SetLastAccessTime(string fullPath, DateTimeOffset time, bool asDirectory)
static FileAttributes GetAttributes(string fullPath)
static void AppendAllText(string path, string? contents)
Definition File.cs:493
static void AppendAllLines(string path, IEnumerable< string > contents, Encoding encoding)
Definition File.cs:542
static void SetCreationTime(string path, DateTime creationTime)
Definition File.cs:152
static void Replace(string sourceFileName, string destinationFileName, string? destinationBackupFileName, bool ignoreMetadataErrors)
Definition File.cs:568
static DateTime GetLastAccessTimeUtc(string path)
Definition File.cs:194
static Task AppendAllLinesAsync(string path, IEnumerable< string > contents, Encoding encoding, CancellationToken cancellationToken=default(CancellationToken))
Definition File.cs:965
static void SetAttributes(string path, FileAttributes fileAttributes)
Definition File.cs:230
static StreamWriter CreateText(string path)
Definition File.cs:29
static FileStream Create(string path)
Definition File.cs:73
static string InternalReadAllText(string path, Encoding encoding)
Definition File.cs:276
static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share)
Definition File.cs:138
static DateTime GetLastAccessTime(string path)
Definition File.cs:188
static FileStream OpenWrite(string path)
Definition File.cs:241
static DateTime GetLastWriteTimeUtc(string path)
Definition File.cs:218
static string[] ReadAllLines(string path, Encoding encoding)
Definition File.cs:373
static void WriteAllLines(string path, string[] contents)
Definition File.cs:434
static bool Exists([NotNullWhen(true)] string? path)
Definition File.cs:97
static void AppendAllText(string path, string? contents, Encoding encoding)
Definition File.cs:507
static Task AppendAllLinesAsync(string path, IEnumerable< string > contents, CancellationToken cancellationToken=default(CancellationToken))
Definition File.cs:960
static void WriteAllLines(string path, string[] contents, Encoding encoding)
Definition File.cs:456
static void SetCreationTimeUtc(string path, DateTime creationTimeUtc)
Definition File.cs:158
static void SetLastWriteTime(string path, DateTime lastWriteTime)
Definition File.cs:200
static FileStream OpenRead(string path)
Definition File.cs:236
static byte[] ReadAllBytesUnknownLength(FileStream fs)
Definition File.cs:1015
static FileStream Open(string path, FileMode mode, FileAccess access)
Definition File.cs:133
static async Task< byte[]> InternalReadAllBytesAsync(FileStream fs, int count, CancellationToken cancellationToken)
Definition File.cs:753
static void SetLastAccessTime(string path, DateTime lastAccessTime)
Definition File.cs:176
static Encoding s_UTF8NoBOM
Definition File.cs:16
static Task WriteAllLinesAsync(string path, IEnumerable< string > contents, CancellationToken cancellationToken=default(CancellationToken))
Definition File.cs:876
static void WriteAllText(string path, string? contents)
Definition File.cs:282
static string ReadAllText(string path, Encoding encoding)
Definition File.cs:259
static void SetLastWriteTimeUtc(string path, DateTime lastWriteTimeUtc)
Definition File.cs:206
static void Replace(string sourceFileName, string destinationFileName, string? destinationBackupFileName)
Definition File.cs:563
static Task< byte[]> ReadAllBytesAsync(string path, CancellationToken cancellationToken=default(CancellationToken))
Definition File.cs:724
static Task WriteAllBytesAsync(string path, byte[] bytes, CancellationToken cancellationToken=default(CancellationToken))
Definition File.cs:810
static string[] ReadAllLines(string path)
Definition File.cs:360
static DateTime GetCreationTime(string path)
Definition File.cs:164
static DateTime GetLastWriteTime(string path)
Definition File.cs:212
static Task< string[]> ReadAllLinesAsync(string path, CancellationToken cancellationToken=default(CancellationToken))
Definition File.cs:836
static StreamReader AsyncStreamReader(string path, Encoding encoding)
Definition File.cs:625
static DateTimeOffset GetUtcDateTimeOffset(DateTime dateTime)
Definition File.cs:143
static StreamWriter AsyncStreamWriter(string path, Encoding encoding, bool append)
Definition File.cs:631
static FileStream Open(string path, FileMode mode)
Definition File.cs:128
static void WriteAllLines(string path, IEnumerable< string > contents)
Definition File.cs:439
static string[] InternalReadAllLines(string path, Encoding encoding)
Definition File.cs:390
static StreamReader OpenText(string path)
Definition File.cs:20
static DateTime GetCreationTimeUtc(string path)
Definition File.cs:170
static FileStream Open(string path, FileStreamOptions options)
Definition File.cs:1004
static void AppendAllLines(string path, IEnumerable< string > contents)
Definition File.cs:525
static IEnumerable< string > ReadLines(string path, Encoding encoding)
Definition File.cs:417
static Task AppendAllTextAsync(string path, string? contents, Encoding encoding, CancellationToken cancellationToken=default(CancellationToken))
Definition File.cs:934
static Task< string[]> ReadAllLinesAsync(string path, Encoding encoding, CancellationToken cancellationToken=default(CancellationToken))
Definition File.cs:841
static Task WriteAllTextAsync(string path, string? contents, Encoding encoding, CancellationToken cancellationToken=default(CancellationToken))
Definition File.cs:698
static async Task< string > InternalReadAllTextAsync(string path, Encoding encoding, CancellationToken cancellationToken)
Definition File.cs:663
static void InternalWriteAllLines(TextWriter writer, IEnumerable< string > contents)
Definition File.cs:482
static Task WriteAllTextAsync(string path, string? contents, CancellationToken cancellationToken=default(CancellationToken))
Definition File.cs:693
static void Copy(string sourceFileName, string destFileName)
Definition File.cs:47
static FileStream Create(string path, int bufferSize, FileOptions options)
Definition File.cs:83
static string ReadAllText(string path)
Definition File.cs:246
static IEnumerable< string > ReadLines(string path)
Definition File.cs:404
static void Move(string sourceFileName, string destFileName, bool overwrite)
Definition File.cs:586
static Task< string > ReadAllTextAsync(string path, Encoding encoding, CancellationToken cancellationToken=default(CancellationToken))
Definition File.cs:642
static async Task< byte[]> InternalReadAllBytesUnknownLengthAsync(FileStream fs, CancellationToken cancellationToken)
Definition File.cs:773
static async Task InternalWriteAllLinesAsync(TextWriter writer, IEnumerable< string > contents, CancellationToken cancellationToken)
Definition File.cs:906
static void WriteAllText(string path, string? contents, Encoding encoding)
Definition File.cs:296
static void Encrypt(string path)
Definition File.cs:614
static FileSystemInfo CreateSymbolicLink(string path, string pathToTarget)
Definition File.cs:990
static SafeFileHandle OpenHandle(string path, FileMode mode=FileMode.Open, FileAccess access=FileAccess.Read, FileShare share=FileShare.Read, FileOptions options=FileOptions.None, long preallocationSize=0L)
Definition File.cs:1009
static FileStream Create(string path, int bufferSize)
Definition File.cs:78
static async Task< string[]> InternalReadAllLinesAsync(string path, Encoding encoding, CancellationToken cancellationToken)
Definition File.cs:862
static void Decrypt(string path)
Definition File.cs:620
static ? FileSystemInfo ResolveLinkTarget(string linkPath, bool returnFinalTarget)
Definition File.cs:998
static StreamWriter AppendText(string path)
Definition File.cs:38
static Task WriteAllLinesAsync(string path, IEnumerable< string > contents, Encoding encoding, CancellationToken cancellationToken=default(CancellationToken))
Definition File.cs:881
static void SetLastAccessTimeUtc(string path, DateTime lastAccessTimeUtc)
Definition File.cs:182
static byte[] ReadAllBytes(string path)
Definition File.cs:314
static Task< string > ReadAllTextAsync(string path, CancellationToken cancellationToken=default(CancellationToken))
Definition File.cs:637
static async Task InternalWriteAllTextAsync(StreamWriter sw, string contents, CancellationToken cancellationToken)
Definition File.cs:920
static void Copy(string sourceFileName, string destFileName, bool overwrite)
Definition File.cs:52
static void WriteAllLines(string path, IEnumerable< string > contents, Encoding encoding)
Definition File.cs:461
static void Move(string sourceFileName, string destFileName)
Definition File.cs:581
static void WriteAllBytes(string path, byte[] bytes)
Definition File.cs:342
static Task AppendAllTextAsync(string path, string? contents, CancellationToken cancellationToken=default(CancellationToken))
Definition File.cs:929
static void Delete(string path)
Definition File.cs:88
static Encoding UTF8NoBOM
Definition File.cs:18
static FileAttributes GetAttributes(string path)
Definition File.cs:224
static bool IsDirectorySeparator(char c)
static string GetFullPath(string path)
Definition Path.cs:881
static unsafe void WriteAtOffset(SafeFileHandle handle, ReadOnlySpan< byte > buffer, long fileOffset)
static ValueTask WriteAtOffsetAsync(SafeFileHandle handle, ReadOnlyMemory< byte > buffer, long fileOffset, CancellationToken cancellationToken, OSFileStreamStrategy strategy=null)
static ReadLinesIterator CreateIterator(string path, Encoding encoding)
static void ValidateArguments(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options, long preallocationSize)
static byte Max(byte val1, byte val2)
Definition Math.cs:738
static string IO_FileTooLong2GB
Definition SR.cs:1566
static string ArgumentNull_FileName
Definition SR.cs:948
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string ArgumentNull_Path
Definition SR.cs:954
static string Argument_EmptyPath
Definition SR.cs:38
static string IO_FileNotFound_FileName
Definition SR.cs:40
static string Argument_EmptyFileName
Definition SR.cs:582
Definition SR.cs:7
static Encoding UTF8
Definition Encoding.cs:526
override string ToString()
StringBuilder Append(char value, int repeatCount)
static Task FromException(Exception exception)
Definition Task.cs:3341
static Task FromCanceled(CancellationToken cancellationToken)
Definition Task.cs:3363
static Task CompletedTask
Definition Task.cs:1120
static void ThrowEndOfFileException()
static DateTime SpecifyKind(DateTime value, DateTimeKind kind)
Definition DateTime.cs:756