Loading a text file

Simon4VB

Member
Joined
Apr 2, 2009
Messages
23
Programming Experience
1-3
Hi,
I have designed a form which includes;
-a textbox1
-a button
-a textbox2 with multiple lines.

My intention is, to input a text file name and location in textbox1 and the button loads the text file in textbox2. Any help please in code.
Thanks,
Simon.
 
I don't know if this will be any help to you or not but I have loaded text files in the past with the following code:

VB.NET:
Dim EntireFile As String
Dim oRead As System.IO.StreamReader

'Reads the file
oRead = IO.File.OpenText(".\TextFileHere.txt")

EntireFile = oRead.ReadToEnd()

I'll try code it for you based on what you have supplied

VB.NET:
Dim EntireFile As String
Dim oRead As System.IO.StreamReader

'Reads the file supplied in the textbox1
oRead = IO.File.OpenText(textbox1.text)

'Reads the entire file into a variable.
EntireFile = oRead.ReadToEnd()

textbox2.text = EntireFile

Give that a try and see if that works, not sure if it will


I have only used this code in the past to read a text file with one line in that is downloaded from the Internet to check a version number so I am not sure if this would handle large text files.. I am sure someone with better knowledge than me can help more :)
 
My intention is, to input a text file name and location in textbox1 and the button loads the text file in textbox2
When was the last time you had to type in the file path to open in any application? Try the OpenFileDialog instead.

Reading the text from file into textbox can also be a single line operation. My.Computer.FileSystem Object

VB.NET:
If Me.OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
    Me.TextBox1.Text = My.Computer.FileSystem.ReadAllText(Me.OpenFileDialog1.FileName)
End If
 
When was the last time you had to type in the file path to open in any application?
Hi John,
You are right its too long. I think that's what you mean.
I need to import the text into a multiple lined text box.
Also I don't wish to set one file path into the coding as I need to import various text files.
 
Back
Top