Skip to content

File.WriteAllTextAsync throws an exception when input string is longer than 4096 characters #22030

@Clearic

Description

@Clearic

Code to reproduce the bug

using System;
using System.IO;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "";
            // Creating string longer than 4096 characters
            while (str.Length < 4096)
            {
                str += "This is a test\n";
            }

            File.WriteAllTextAsync("test.txt", str).Wait();

            Console.WriteLine("Done");
        }
    }
}

output

Unhandled Exception: System.AggregateException: One or more errors occurred. (Index and count must refer to a location within the string.
Parameter name: sourceIndex) ---> System.ArgumentOutOfRangeException: Index and count must refer to a location within the string.
Parameter name: sourceIndex
   at System.String.CopyTo(Int32 sourceIndex, Char[] destination, Int32 destinationIndex, Int32 count)
   at System.IO.File.<InternalWriteAllTextAsync>d__81.MoveNext()
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at System.Threading.Tasks.Task.Wait()
   at test6.Program.Main(String[] args)

dotnet --info

.NET Command Line Tools (2.0.0-preview1-005899)

Product Information:
 Version:            2.0.0-preview1-005899
 Commit SHA-1 hash:  ea746bd0dd

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.15063
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Users\Alexandr\.dotnet\x64\sdk\2.0.0-preview1-005899\

Microsoft .NET Core Shared Framework Host

  Version  : 2.0.0-preview1-002111-00
  Build    : 1ff021936263d492539399688f46fd3827169983

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions