0

test.txt content,

enter image description here

Now I am trying to copy partial content of file (example, only 2nd line Hello1) and using below. fileStream.Seek

{
         Get().Wait();
    }


    private static HttpClient Client { get; } = new HttpClient();
    public static  async Task<HttpResponseMessage> Get()
    {
        using (var fileStream = new FileStream(@"C:\Files\test.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        {

            fileStream.Seek(5, SeekOrigin.Current);

            var result = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StreamContent(fileStream),
            };

            using (FileStream fs = new FileStream(@"C:\Files\test_Copy.txt", FileMode.CreateNew, FileAccess.Write))
            {

                var X = result.Content;

                await result.Content.CopyToAsync(fs);
            }

            return result;
        }

    }

The above code copy only partial content (Hello1), but I am also seeing a line above it.

enter image description here

I would like the copy file (text_copy.txt) should start with line 1. How to do it? Thanks!

user584018
  • 10,186
  • 15
  • 74
  • 160

2 Answers2

3

I think this is due to how windows treats new lines. crlf. If you start at 6, I believe you won't see new line.

CodeNinja
  • 94
  • 1
  • 6
0

There are more than 5 characters in the first line. Apart from the letter of "Hello", there is also the line break character (or even two for windows files). If you want to read the file line by line, it would probably be easier to use File.ReadLines (see also here).

EwgB
  • 61
  • 2
  • 4