Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1415
  • Last Modified:

ReadToEnd() and then rewind.

If I use ReadToEnd() to read a StreamReader object, how can I rewind back to the start of the stream so I can ReadToEnd() again. Thanks.
0
DColin
Asked:
DColin
  • 3
  • 2
1 Solution
 
ThaloxCommented:

to read your stream again, you just need to reset the position of your stream

MemoryStream ms = new MemoryStream();

ms.Seek(0, SeekOrigin.Begin);
StreamReader sr = new StreamReader(ms);

// first read
sr.ReadToEnd();

// reset position
ms.Seek(0, SeekOrigin.Begin);

read again
sr.ReadToEnd();

best luck

Thalox
0
 
DColinAuthor Commented:
Thalox,
I tried your suggestion and was told seek in not a member of StreamReader. Here is what I am trying to do, hopefully it is self explanatory. I don't suppose you could also tell me how to save the banner.html file after as well.


Dim HTMLTemplate As StreamReader

HTMLTemplate = File.OpenText(Server.MapPath("Banner.HTML"))
HTMLTemplate.ReadToEnd().Replace("Name1", "Adam")
HTMLTemplate.ReadToEnd().Replace("Name2", "Brian")
HTMLTemplate.ReadToEnd().Replace("Name3", "Colin")
HTMLTemplate.ReadToEnd().Replace("Name4", "Dan")

HTMLTemplate.close
0
 
chaniewskimCommented:
Hmm...
You are replacing strings, but the results go nowhere.

Try this:

HTMLTemplate = File.OpenText(Server.MapPath("Banner.HTML"))
string Template = HTMLTemplate.ReadToEnd()
Template = Template.Replace("Name1", "Adam")
Template = Template.Replace("Name2", "Brian")
Template = Template.Replace("Name3", "Colin")
Template = Template.Replace("Name1", "Dan")
HTMLTemplate.close

Then you can do with your Template string whatever you need.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
chaniewskimCommented:
Sorry for two posts.
This way you don't need to rewind your file. Using HTMLTemplate.ReadToEnd().Replace("Name1", "Adam") you are also creating a temporary string object, so there is no performance penalty in my solution - in fact it performs better, because you don't read that file with every operation.
0
 
ThaloxCommented:

i think chaniewskim is right, this method is better than read the stream more than once

but if you want to use this:

the "seek()" - method belongs to Stream, not streamreader.
for your problem you should open the file in a stream

FileStream fs = new FileStream(Server.MapPath("Banner.HTML"), FileAccess.Read);

now you can read it with the streamreader

StreamReader HTMLTemplate = new StreamReader(fs);

fs.Seek(0, SeekOrigin.Begin);
HTMLTemplate.ReadToEnd().Replace("Name1", "Adam")

fs.Seek(0, SeekOrigin.Begin);
HTMLTemplate.ReadToEnd().Replace("Name2", "Brian")

fs.Seek(0, SeekOrigin.Begin);
HTMLTemplate.ReadToEnd().Replace("Name3", "Colin")
....


but i'm not sure if this will replace all strings.
to read it in a string and replace things there would be better
0
 
chaniewskimCommented:
In fact,

fs.Seek(0, SeekOrigin.Begin);
HTMLTemplate.ReadToEnd().Replace("Name1", "Adam")

fs.Seek(0, SeekOrigin.Begin);
HTMLTemplate.ReadToEnd().Replace("Name2", "Brian")

fs.Seek(0, SeekOrigin.Begin);
HTMLTemplate.ReadToEnd().Replace("Name3", "Colin")
....


Does exactly _nothing_, but in time-costly manner. It replaces those names and discards the results.
DColin, do you want to change those names in the file? You have to do this using temporary string and then _rewrite_ the file with result. Anyway, you have to use a temporary variable to do something with results of those operations.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now