Can you help me describe, in laymens terms, how to read a file using a StreamReader object using Visual Basic. Thanks for your help community.Can you describe in easy to understand terms how to a read a file using a StreamReader object using VB?
1. First make sure you have a Stream object somewhere, this can be a NetworkStream or any other stream you already have open. I will assume you have a Stream called MyStream.
2. You can use the following code:
Dim MyReader as New StreamReader(MyStream)
where MyReader will be the name of your new StreamReader, and MyStream is the stream you already had.
3. You can then read out the stream using one of the following:
* MyReader.Read() this reads out the next character in the stream and moves the pointer by one character (this means the next Read() will read the character after the character you have just read).
* MyReader.ReadLine() this reads until the next line break (newline) character, and returns a String with what has just been read.
* MyReader.ReadToEnd() this will read all content in the stream (I am not sure how this works for NetworkStream objects, but when reading a file it will return all content in the file, including any newlines) and return it as a String.
Other functions to manipulate or read the string are available, but the above three functions are those that you will need most for basic usage, including the MyReader.EndOfStream property which returns a boolean (true/false) indicating whether the end of the Stream has been reached. If this is the case, you have read everything, and are probably done with what you are doing. This is not needed when using ReadToEnd, since you will always end up reading everything.
For a complete reference of the StreamReader you can read the following page: http://msdn.microsoft.com/en-us/library/system.io.streamreader_members.aspx
Good luck with your project :)
No comments:
Post a Comment