I need to replace the windows EOL "\r\n" with just "\n", and I want to this in the fastest way possible, as the software will have a lot of files to upload, many of them with a few thousands lines. My current approach is:

UnicodeEncoding uniEncoding = new UnicodeEncoding();
using (MemoryStream newStream = new MemoryStream())
    using (TextReader stream = new StreamReader(fileInfo.Open(FileMode.Open, FileAccess.Read)))
        string line = null;
        while ((line = await stream.ReadLineAsync()) != null)
            line += "\n";
            await newStream.WriteAsync(uniEncoding.GetBytes(line), 0, line.Length);

    client.UploadFile(newStream, remote, true);

Another approach I was considering was to wrap the Stream I get from the file, and just not return the \n when read. I think this would consume less memory, but I am not sure if this would cause more problems as I have never really worked with streams.

Any feedback on my current approach / alternatives would be appreciated.

  • 1
    \$\begingroup\$ Have you tested that this works? It looks like the call to WriteAsync is incorrect - it should be the number of bytes to write, not the number of chars. \$\endgroup\$
    – RobH
    Aug 1, 2016 at 13:15
  • \$\begingroup\$ I'm curious about your requirement, you indicate you want to replace "\r\n" with "\n", but would it be sufficient to simply strip all "\r" characters? Is it valid to have a "\r" character not followed by a "\n"? \$\endgroup\$
    – Snixtor
    Aug 2, 2016 at 8:13

1 Answer 1



  • The non-Async methods are much faster in case of ReadLine/Write. If you need to run the code in background, use await Task.Run(/* put the whole code here */);


  • You can use the static property Encoding.Unicode. (It is the same as creating a UnicodeEncoding instance without constructor arguments)
  • Add the Encoding to the stream reader: new StreamReader(fileInfo.Open(FileMode.Open, FileAccess.Read), Encoding.Unicode). Otherwise, if the input file is not unicode encoded, the output becomes a bunch of trash.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.