Solved

ReadToEnd() and then rewind.

Posted on 2004-04-26
6
1,407 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

932 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

15 Experts available now in Live!

Get 1:1 Help Now