C#: looking for file stream seek between 2 length

安稳与你 提交于 2019-12-11 15:28:43

问题


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,

来源:https://stackoverflow.com/questions/49091374/c-looking-for-file-stream-seek-between-2-length

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!