How do I make this work in vb.net?
I am trying to read/write a simple text file in net 2008 but can't, as yet, get it to work.
Any help would be appreciated.
This is how I use to do it in VB6
Code:
MyStr = App.Path & "\Data\Data.txt"
Open MtyStr For Input As #1
Do While Not EOF(1)
Input #1, Num(a, 0), Num(a, 1), Num(a, 2), Num(a, 3), Num(a, 4), Num(a, 5), Num(a, 6)
a = a + 1
Loop
Close #1
Re: How do I make this work in vb.net?
Look up the StreamReader() and StreamWriter() classes. That's the way to access files in VB.Net
Try these samples, also: http://code.msdn.microsoft.com/vbsamples/
Re: How do I make this work in vb.net?
I found info about the streamreader/writer in my book and got it to work, the problem is that it reads/ writes the whole thing, I was looking for a way to read data individually separated by commas. Would you know of a simple way to do this?
Re: How do I make this work in vb.net?
The best way or at least a way I often use is to read the entire file and then use the split method to place it into an array. This works very well and is very fast.
Re: How do I make this work in vb.net?
And there's me thinking it was going to be so easy.
Just need to find something helpful on splitting now.:sick:
Re: How do I make this work in vb.net?
It is actually very easy.
Use the streamreader to read either a line or the entire file into a string variable.
Dim an array and use the split function on your previous variable to break it down into each element.
Code:
Dim MyFile as new System.IO.StreamReader("\MyFile.Txt")
Dim FileContent as string=MyFile.ReadToEnd()
MyFile.Close
Dim FileFields as string()=FileContent.Split(",")
Re: How do I make this work in vb.net?
Cool, thank you.
This is my code so far and it seems to be working very well except for the splitting bit.
Code:
a = 0
Dim objfile As New System.IO.StreamReader("c:\data.txt")
Nationaldate = objfile.ReadLine()
Do Until Nationaldate Is Nothing
ListNational.Items.Add(Nationaldate)
Nationaldate = objfile.ReadLine()
a = a + 1
Loop
objfile.Close()
objfile.Dispose()
Label1.text= "Line Count = " & a+1
It loads in over 1000 lines, each line has 7 data entries.
How would I insert the split command to read the 7 separate data entries into a array at each loop?
Re: How do I make this work in vb.net?
Split Nationaldate from inside the loop, and load the array (based on the value of a)
(a,0),(a,1),(a,3)
Re: How do I make this work in vb.net?
The examples I have are a little unclear as to the wording or layout I would use to split it into a array (a, 0 ) through to (a,6) and my first few attempts have failed.
Re: How do I make this work in vb.net?
VB.NET String Functions
I hope it helps a bit.
Re: How do I make this work in vb.net?
Yeehee!! I have done it, and it works like a charm :)
Thank you so much for all of you who took time to help, it is very much appreciated.
Now that I have learned to load and save I can move on to other heroic tasks.:D
Re: How do I make this work in vb.net?
Let me quote Neil Armstrong :
"One small step for Dajunka; one giant leap for developers" ;) :)