C#: looking for file stream seek between 2 length

2019-08-22 00:05发布

问题:

I would like to copy the partial content of a file using FileStream seek.

  1. the actual file stream length of the file = 98764.
  2. I want file content between length 200 and 5000
  3. If I am doing fileStream.Seek(200, SeekOrigin.Begin), then this give me file content from 200 to 98764, but I want from 200 and 5000.

    public static async Task<HttpResponseMessage> Get()
    {
        using (var fileStream = new FileStream(@"C:\test\tes1.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        {
            var actualFileStremSize = fileStream.Length;
    
            //fileStream.Seek(200, SeekOrigin.Begin);
    
            var result = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StreamContent(fileStream),
            };
    
            using (FileStream fs = new FileStream(@"C:\test\tes1_partial_copy.txt", FileMode.CreateNew, FileAccess.Write))
            {
                await result.Content.CopyToAsync(fs);
            }
    
            return result;
        }
      }
      }
    

Main Method,

     class Program
    {
    static void Main(string[] args)
    {
        Get().Wait();
        Console.ReadLine();
    }

Looking for a solution to get file content between 2 file stream length. Thanks,

标签: c# filestream