|
-
Mar 14th, 2008, 08:57 AM
#1
Thread Starter
Addicted Member
-
Mar 14th, 2008, 09:01 AM
#2
Re: Read certain Lines from .txt file
You're reading the file and splitting it into lines, but then you only get one of those lines and throw the rest away. How about you keep all the lines. Then you can access any one you want any time. There's a far more elegant way to get the lines too:
vb.net Code:
Dim lines As String() = IO.File.ReadAllLines("file path here")
Now you can access any line you want from the 'lines' array at your leisure.
EDIT: Thanks stanav, but I might have to rep you down for finding fault in my post.
Last edited by jmcilhinney; Mar 14th, 2008 at 09:09 AM.
-
Mar 14th, 2008, 09:08 AM
#3
Re: Read certain Lines from .txt file
Just a minor correction on JMC's code. It should be this
Code:
Dim lines() As String = IO.File.ReadAllLines("file path here")
'Or this
Dim lines As String() = IO.File.ReadAllLines("file path here")
-
Mar 14th, 2008, 10:21 AM
#4
Thread Starter
Addicted Member
Re: Read certain Lines from .txt file
I have put your code but in the text file is showing me "System.String[]"
Code:
Dim Tr As IO.TextReader = System.IO.File.OpenText(My.Settings.masuratori_preluare_date)
Dim TW As System.IO.TextWriter
Dim nume_fisier As String
'Dim MyFileLine As String = Split(Tr.ReadToEnd(), vbCrLf)(1) & vbCrLf
Dim MyFileLines As String() = IO.File.ReadAllLines(My.Settings.masuratori_preluare_date)
nume_fisier = My.Settings.masuratori_salvare_date & Format(Date.Now.Day) & "-" & Format(Date.Now.Month) & "-" & Format(Date.Now.Year) & " la " & Format(Date.Now.Hour) & "." & Format(Date.Now.Minute) & "." & Format(Date.Now.Second) & "s" & ".txt"
Tr.Close()
TW = System.IO.File.CreateText(nume_fisier)
TW.WriteLine(MyFileLines)
TW.Flush()
TW.Close()
I'm still learning VB.NET
Sorry for my bad english
Thanks for your help
-
Mar 14th, 2008, 10:38 AM
#5
Re: Read certain Lines from .txt file
You have to join the array before you can write it to the file.
vb.net Code:
TW.WriteLine(String.Join(ControlChars.CrLf, MyFileLines))
-
Mar 14th, 2008, 10:46 AM
#6
Thread Starter
Addicted Member
-
Mar 14th, 2008, 10:56 AM
#7
Re: Read certain Lines from .txt file
We showed you how to read the contents of a file into a string array where each element holds a line of your text. Now if you want to read a specific line, you just read it from the array at a specific index. Arrays are zero based, so for example, if you want line#3 in your text file, you will read the array element at index#2.
Code:
Dim lines As String() = IO.File.ReadAllLines("path here")
Dim line3 As String = lines(2)
One thing you have to keep in mind that if you're trying to access an element that is not in the array, you program will crap out.
-
Mar 14th, 2008, 11:33 AM
#8
Thread Starter
Addicted Member
-
Mar 23rd, 2011, 12:59 PM
#9
New Member
Re: [RESOLVED] Read certain Lines from .txt file
thank you all guys for helping me also.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|