How do I read a Stream and reset its position to zero even if stream.CanSeek == false? I need some work around.
How do I read a Stream and reset its position to zero even if
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If your scenario permits you to replace your original stream, then you could check whether it supports seeking and, if not, read its content and wrap them into a new
MemoryStream, which you could then use for subsequent operations.The above is rather inefficient, since it must allocate memory for twice the size of your content. My recommendation would be to adapt the parts of your code where you’re accessing the stream (after having read all its content) in order to make them access the stored copy of your content instead. For example:
Since the
StringReaderjust reads from yourcontentstring, you would not waste memory creating redundant copies of your data.