: At the top of my app I have a label control which will display a line of text. This will work like an ad-rotator but will only display text quotes. I want to list the quotes inside a text file e.g:
: "Help yourself now"
: "Get free now"
: "Have a nice day"
: ... etc
: When the user clicks on a button it will display the next quote. I've now idea how to do this. All I know is that I can use a FileStream object (fS) to access the Position property - this will give me the position of the stream (i.e. fS.Position) and I also know that I can get the length of the stream with the length property (i.e. fS.Length).
: I think I should try and check how many lines are in the file but am unsure how to continue.
: How can I complete the task? Please help. Many thanks.
For my suggestion I will assume that u are the writer of the text file so that u can edit it the way that u prefer (and that will make the code work ;)
U need to write a text file as u would normally do BUT in the end of every message u should add an escape character. With this I mean a character u will never use on the message (here I used @, but remember u have all the unicode set to chose from :))
2 important notes:
1 .Peek() and .Read() returns an int... so u have to do the cast explicitely
2 for the way the code is written u have to add the escape char also at the end of the last line
the following code (it will works if u compile it) will read a file, parse the stream and add the messages in the correct position.
For quickness here I used a fixed size array (I imagine u know the exact number of messages to store). In case..u can use a dynamic collection.
public class ManageAdRotator
// the array where u can record the messages
private string zMessage;
private int i;
i = 0;
zMessage = new string;
public void ShowNextAd()
// this should be put in a try&catch to be sure u don't throw
// an unwanted exception in case the file cannot be found or
using (StreamReader sr = new StreamReader("Text.txt"))
// with peek() u look at the following char without "taking it"
// here we will parse all the stream untill the end (char 0)
while (sr.Peek() >= 0)
// the first read if for not reading @
// the 2 following ones are for not recording in the string
// the carry and end of line char
if (sr.Peek() == 64) // 64 == @
// a test line to be sure the application works
// we found the escape char, so we say the prog to begin
// a new message
// this is to skip the @ char and not store it in the
// so u skip the end of line and carry chars
// if everything is ok, u append the next char on the
// appropriate message string
zMessage[i] = zMessage[i] + (char)sr.Read();
public class Starter
public static void Main()
ManageAdRotator zManage = new ManageAdRotator();
I hope this is what u need.