Using StreamReader to get text

I am tring to get the contents of a text file using StreamReader.  Not sure if it is considered UniCode or ASCII.
I tried changing the Encoding to various things (UniCode, <default>, ASCII)  it just returns a number like 126 or 1509.  My StreamWriter Function works perfect (i.e.  A File that says "Hello World" is 12 Bytes), but the StreamReader doesn't

I just a simple Function to give the contents of a File into a String,
strMyText = ReadFile("f:\test.txt")

Thank You Very Much in Advance
 


Function ReadFile(ByVal FullLocation As String) As String
        Dim srFile As StreamReader
        srFile = New StreamReader(FullLocation)
        ReadFile = srFile.Read()
        srFile.Close()
    End Function

Open in new window

JPTenoreAsked:
Who is Participating?
 
Jaime OlivaresConnect With a Mentor Software ArchitectCommented:
typo!!! my L key is not working rigth:

ReadFile = File.ReadAllText("f:\text.txt")

0
 
Jaime OlivaresSoftware ArchitectCommented:
you can use:

string text = ReadAllText(@"f:\text.txt");
0
 
JPTenoreAuthor Commented:
ReadAllText  humm???

Function ReadFile(ByVal FullLocation As String) As String
        Dim srFile As StreamReader
        srFile = New StreamReader(FullLocation)
        ReadFile = srFile.ReadAllText()
        srFile.Close()
End Function
Is this right?

I also have another question pending if you want to answer it:)
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Jaime OlivaresSoftware ArchitectCommented:
no need, ReadAllText replaces all your code.
BTW, remove the @, it is needed just in C#, not vb.net
0
 
JPTenoreAuthor Commented:
Hold on let me try it :)
0
 
JPTenoreAuthor Commented:
It doesn't like ReadAllText as a VB.NET Function???

ReadFile = ReadAllText("f:\text.txt")

Open in new window

0
 
Jaime OlivaresSoftware ArchitectCommented:
oops, sorry, it is:

ReadFile = File.ReadAlText("f:\text.txt")

or

ReadFile = System.IO.File.ReadAlText("f:\text.txt")
0
 
JPTenoreAuthor Commented:

I have Imports System.IO up top
I guess you have to Fully Declear

I will give you Credit in a moment on this one you definitity Solved It, with a A Grade!

Hey, do you know anything on DataSets and TableAdapters?  I have another, and I have a project for you send I email to JP+winsupplychain    in the com domain    (and replace + with @) get it?



0
 
JPTenoreAuthor Commented:
sorry TYPO a NOT I
0
 
Jaime OlivaresSoftware ArchitectCommented:
if you have Imports System.IO, then you need:
ReadFile = File.ReadAlText("f:\text.txt")
0
 
JPTenoreAuthor Commented:
ReadFile = System.IO.File.ReadAllText("f:\text.txt")


You had a TYPO too  ReadAllText NOT ReadAlText

CutnPaste it into another Comment so the next person has a Working Solution, I will Accept that one.

Thanks.


0
 
JPTenoreAuthor Commented:
OPPS didn't see that has a comment I will approve that in a second, but first  do you see my other question?
0
 
JPTenoreAuthor Commented:
TYPO as NOT has
0
 
JPTenoreAuthor Commented:
Can you send me an e-mail?

Be back in 30mins. Do you have a moment to look at my other Question?
0
 
Jaime OlivaresSoftware ArchitectCommented:
So, did it work?
I will look the other question...
0
 
JPTenoreAuthor Commented:
Yes, let me try again with Accepting
0
 
JPTenoreAuthor Commented:
If you have
Imports System.IO

you need just 'File' NOT it fully decleared 'System.IO.File'
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.