Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ReadToEnd() and then rewind.

Posted on 2004-04-26
6
Medium Priority
?
1,414 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

670 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