S
S
SHentai2018-03-10 14:33:46
C++ / C#
SHentai, 2018-03-10 14:33:46

StreamReader how to change position?

Hello.
It is necessary to change the position in the stream, immediately, for example, to the 100th byte, but I can’t find examples where someone changed the position at all, it says that BaseStream.Position can both be read and set its own value, but when setting it, I get errors:

System.InvalidOperationException: You cannot modify the position of a FtpDataStream. This property is updated as data is read or written to the stream.
   at FluentFTP.FtpDataStream.set_Position(Int64 value)

var client = await FTPConnect(userData);
                using (StreamReader sr = new StreamReader(await client.OpenReadAsync(path), Encoding.UTF8))
                {
                    try
                    {
                        sr.BaseStream.Position = Convert.ToInt64(offset); // Convert.ToInt64, поскольку нужно long
                        // for (int i = 0; i != offset; i++) sr.Read(); // Сделал с циклом, но не думаю что это хорошо
                        Console.WriteLine("#" + sr.BaseStream.Position + "\n" + sr.ReadToEnd() + "\n#"+sr.BaseStream.Position); 
                    }
                    finally
                    {
                        sr.Close();
                    }
                }

I would be grateful for any help!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Yudakov, 2018-03-10
@SHentai

The download start position must be specified in the "ContentOffset" property of the "FtpWebRequest" object:
https://msdn.microsoft.com/en-us/library/system.ne...
The Stream does not need to be touched. There will already be data from the position we need.

C
cicatrix, 2018-03-10
@cicatrix

Network streams do not support the SEEK operation. Read everything in an array, and already there select the right one.
This can only work with File / MemoryStream
Before setting the position, check the Stream.CanSeek property

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question