| | |
How to retrieve data from a .txt file
Please support our VB.NET advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
•
•
' Read the whole file to a string
FileText = My.Computer.FileSystem.ReadAllText("D:\account.txt")
The above line will read the whole file at once. if the file is big it may lead to memory problem, I prefer to read line by line, I guess my code will be faster on PII with 64mb ram.
•
•
•
•
The above line will read the whole file at once. if the file is big it may lead to memory problem, I prefer to read line by line, I guess my code will be faster on PII with 64mb ram.
And after all, it's a question about selecting the right "tool"
Teme64 @ Windows Developer Blog
•
•
Join Date: May 2009
Posts: 14
Reputation:
Solved Threads: 0
•
•
•
•
To get those values to text boxes goes like this
Now TextBox1 should show "Account" and TextBox2 should show " Apple".VB.NET Syntax (Toggle Plain Text)
TextBox1.Text = OneLine(0) TextBox2.Text = OneLine(1)
That explanation totally cleared things up for me, thankyou so much! now my text box is showing text from the file but its showing the whole last line. This is what i have:
VB.NET Syntax (Toggle Plain Text)
Private Sub Fill() If My.Computer.FileSystem.FileExists("C:\Travian.txt") Then FileText = My.Computer.FileSystem.ReadAllText("C:\Travian.txt") FileLines = FileText.Split(CChar(Environment.NewLine)) SeparatorChar = "," For i = 0 To FileLines.GetUpperBound(0) OneLine = FileLines(i).Split(CChar(SeparatorChar)) Next i End If txtUser.Text = OneLine(0) End Sub
•
•
•
•
but its showing the whole last line.
•
•
•
•
have i given the wrong index for OneLine?
Can you tell me, for what purpose this
VB.NET Syntax (Toggle Plain Text)
For i = 0 To FileLines.GetUpperBound(0) OneLine = FileLines(i).Split(CChar(SeparatorChar)) Next i
VB.NET Syntax (Toggle Plain Text)
OneLine = FileLines(0).Split(CChar(SeparatorChar)) txtUser.Text = OneLine(0)
Or post the data you're using and explain how you should display it in the form.
Teme64 @ Windows Developer Blog
•
•
Join Date: May 2009
Posts: 14
Reputation:
Solved Threads: 0
Ok so my form looks like this
Account: _______
Username: ______
Password: ______
The account, username and password are labels and the ____ represent textboxes named appropriately. When i save the data it gets put into a text document that is named like this: txtAccount.Text & ".txt"
and the data in the text file is like this:
Account: Apple
Username: Orange
Password: Banana
basically what happens now is that it is only looking for one file (but ill fix it to look for the user specified file later) and then like you said it reads the file, splits it into lines then splits each line into two parts. I want the bit after the seperator to appear in the text box basically and so far its just putting the last line of the text file in. I also changed the value of OneLine to (1) but then it said the array was out of bounds so im thinking its actually starting to read from the last line of the text file.
Account: _______
Username: ______
Password: ______
The account, username and password are labels and the ____ represent textboxes named appropriately. When i save the data it gets put into a text document that is named like this: txtAccount.Text & ".txt"
and the data in the text file is like this:
Account: Apple
Username: Orange
Password: Banana
basically what happens now is that it is only looking for one file (but ill fix it to look for the user specified file later) and then like you said it reads the file, splits it into lines then splits each line into two parts. I want the bit after the seperator to appear in the text box basically and so far its just putting the last line of the text file in. I also changed the value of OneLine to (1) but then it said the array was out of bounds so im thinking its actually starting to read from the last line of the text file.
Ok, you have three lines which you need to show (didn't remember your first post). Your code should be something like this
Check that SeparatorChar variable has the correct value (it should be ":" I think, now your code is using ",").
It starts from the first line. Like I commented in the code above, it all depends if you have a single
Account: Apple
Username: Orange
Password: Banana
or multiple Account, Username and Password lines. In that case, the code will loop and use the last "triple".
VB.NET Syntax (Toggle Plain Text)
' Assuming you have multiple "triple" entries in your file For i = 0 To FileLines.GetUpperBound(0) Step 3 OneLine = FileLines(i).Split(CChar(SeparatorChar)) txtAccount.Text = OneLine(1).Trim OneLine = FileLines(i + 1).Split(CChar(SeparatorChar)) txtUserName.Text = OneLine(1).Trim OneLine = FileLines(i + 2).Split(CChar(SeparatorChar)) txtPassword.Text = OneLine(1).Trim Next i ' Or with a single entry in the file (no loop needed) OneLine = FileLines(0).Split(CChar(SeparatorChar)) txtAccount.Text = OneLine(1).Trim OneLine = FileLines(1).Split(CChar(SeparatorChar)) txtUserName.Text = OneLine(1).Trim OneLine = FileLines(2).Split(CChar(SeparatorChar)) txtPassword.Text = OneLine(1).Trim
•
•
•
•
I also changed the value of OneLine to (1) but then it said the array was out of bounds
•
•
•
•
im thinking its actually starting to read from the last line of the text file.
Account: Apple
Username: Orange
Password: Banana
or multiple Account, Username and Password lines. In that case, the code will loop and use the last "triple".
Last edited by Teme64; Jun 4th, 2009 at 6:01 am. Reason: Pressed Submit button by mistake
Teme64 @ Windows Developer Blog
Hi! Nice to hear that you got answer to your problem. Could you please mark the thread as solved. Thank you!
If you run in to a problem you can't solve, when looking for the files, start a new thread for that, please.
P.s. take a look at OpenFileDialog control...
If you run in to a problem you can't solve, when looking for the files, start a new thread for that, please.
P.s. take a look at OpenFileDialog control...
Teme64 @ Windows Developer Blog
![]() |
Similar Threads
- Capturing data from txt file using JavaScript/HTML (JavaScript / DHTML / AJAX)
- Reading hex data from .txt file and display in excel sheet (VB.NET)
- reading a specific data in .txt file to put in specific tag in xml in vb.net (VB.NET)
- Simple login (read data through txt file) (Visual Basic 4 / 5 / 6)
- how can i stop overwriting my data when outputing to a txt file? (C++)
- help: retrieve data from a XML file into a vb form (Visual Basic 4 / 5 / 6)
- Extracting a field in a txt file (C)
- How can I read from a txt file? (C++)
Other Threads in the VB.NET Forum
- Previous Thread: insert value 000011 into sql database
- Next Thread: Runing exe from UNC
Views: 1141 | Replies: 17
| Thread Tools | Search this Thread |
Tag cloud for VB.NET
"crystal .net .net2005 2008 access add application array assignment basic box button buttons center class click code combo convert cpu data database datagrid datagridview design designer dissertation dissertations dissertationthesis dosconsolevb.net editvb.net employees error excel exists firewall function image images isnumericfuntioncall listview login map math memory mobile module msaccess mssqlbackend mysql navigate net opacity page pan picturebox port print printing printpreview problem record refresh regex reports" reuse right-to-left save savedialog search serial socket sorting sql sqldatbase storedprocedure string structures studio temp textbox timer txttoxmlconverter upload useraccounts usercontol usercontrol vb vb.net vb.nettoolboxvisualbasic2008sidebar vb2008 vbnet vista visual visualbasic visualbasic.net visualstudio2008 web wpf xml





