Solved

ReadToEnd() and then rewind.

Posted on 2004-04-26
6
1,408 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

770 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