Solved

# Using StreamReader to get text

Posted on 2008-10-06
599 Views
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,

Thank You Very Much in Advance

Function ReadFile(ByVal FullLocation As String) As String

Dim srFile As StreamReader

srFile = New StreamReader(FullLocation)

srFile.Close()

End Function

0
Question by:JPTenore
• 11
• 6

LVL 55

Expert Comment

you can use:

string text = ReadAllText(@"f:\text.txt");
0

Author Comment

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

I also have another question pending if you want to answer it:)
0

LVL 55

Expert Comment

no need, ReadAllText replaces all your code.
BTW, remove the @, it is needed just in C#, not vb.net
0

Author Comment

Hold on let me try it :)
0

Author Comment

It doesn't like ReadAllText as a VB.NET Function???

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

0

LVL 55

Expert Comment

oops, sorry, it is:

or

0

Author Comment

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

Author Comment

sorry TYPO a NOT I
0

LVL 55

Expert Comment

if you have Imports System.IO, then you need:
0

LVL 55

Accepted Solution

Jaime Olivares earned 500 total points
typo!!! my L key is not working rigth:

0

Author Comment

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

Thanks.

0

Author Comment

OPPS didn't see that has a comment I will approve that in a second, but first  do you see my other question?
0

Author Comment

TYPO as NOT has
0

Author Comment

Can you send me an e-mail?

Be back in 30mins. Do you have a moment to look at my other Question?
0

LVL 55

Expert Comment

So, did it work?
I will look the other question...
0

Author Comment

Yes, let me try again with Accepting
0

Author Closing Comment

If you have
Imports System.IO

you need just 'File' NOT it fully decleared 'System.IO.File'
0

## Join & Write a Comment Already a member? Login.

### Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

#### 728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

#### Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!