Solved

ReadToEnd() and then rewind.

Posted on 2004-04-26
6
1,405 Views
Last Modified: 2008-02-01
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
Comment
Question by:DColin
  • 3
  • 2
6 Comments
 
LVL 9

Expert Comment

by:Thalox
ID: 10925760

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
 

Author Comment

by:DColin
ID: 10926092
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
 
LVL 5

Accepted Solution

by:
chaniewskim earned 500 total points
ID: 10926318
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 5

Expert Comment

by:chaniewskim
ID: 10926332
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
 
LVL 9

Expert Comment

by:Thalox
ID: 10926704

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
 
LVL 5

Expert Comment

by:chaniewskim
ID: 10926720
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now