Solved

Using StreamReader to get text

Posted on 2008-10-06
17
604 Views
Last Modified: 2012-06-21
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

0
Comment
Question by:JPTenore
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 6
17 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22655684
you can use:

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

Author Comment

by:JPTenore
ID: 22655760
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22655804
no need, ReadAllText replaces all your code.
BTW, remove the @, it is needed just in C#, not vb.net
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

Author Comment

by:JPTenore
ID: 22655842
Hold on let me try it :)
0
 

Author Comment

by:JPTenore
ID: 22655868
It doesn't like ReadAllText as a VB.NET Function???

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

Open in new window

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22655908
oops, sorry, it is:

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

or

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

Author Comment

by:JPTenore
ID: 22655977

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

by:JPTenore
ID: 22655982
sorry TYPO a NOT I
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22656016
if you have Imports System.IO, then you need:
ReadFile = File.ReadAlText("f:\text.txt")
0
 
LVL 55

Accepted Solution

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

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

0
 

Author Comment

by:JPTenore
ID: 22656048
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
 

Author Comment

by:JPTenore
ID: 22656051
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

by:JPTenore
ID: 22656058
TYPO as NOT has
0
 

Author Comment

by:JPTenore
ID: 22656089
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

by:Jaime Olivares
ID: 22656115
So, did it work?
I will look the other question...
0
 

Author Comment

by:JPTenore
ID: 22656124
Yes, let me try again with Accepting
0
 

Author Closing Comment

by:JPTenore
ID: 31503677
If you have
Imports System.IO

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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

707 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