diff --git a/src/libraries/System.Private.CoreLib/src/System/IO/File.cs b/src/libraries/System.Private.CoreLib/src/System/IO/File.cs index 1f24874266c45..d4aa96ace0e88 100644 --- a/src/libraries/System.Private.CoreLib/src/System/IO/File.cs +++ b/src/libraries/System.Private.CoreLib/src/System/IO/File.cs @@ -921,6 +921,7 @@ private static async Task InternalWriteAllLinesAsync(TextWriter writer, IEnumera private static async Task InternalWriteAllTextAsync(StreamWriter sw, string contents, CancellationToken cancellationToken) { +#if MS_IO_REDIST char[]? buffer = null; try { @@ -931,11 +932,7 @@ private static async Task InternalWriteAllTextAsync(StreamWriter sw, string cont { int batchSize = Math.Min(DefaultBufferSize, count - index); contents.CopyTo(index, buffer, 0, batchSize); -#if MS_IO_REDIST await sw.WriteAsync(buffer, 0, batchSize).ConfigureAwait(false); -#else - await sw.WriteAsync(new ReadOnlyMemory(buffer, 0, batchSize), cancellationToken).ConfigureAwait(false); -#endif index += batchSize; } @@ -950,6 +947,13 @@ private static async Task InternalWriteAllTextAsync(StreamWriter sw, string cont ArrayPool.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))