954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

FIle IO - Storing data into an Array help.

Hi,

I've been searching around these forums for quite a while now, and can't seem to either understand or find what I'm looking for.

^ As you can probably tell, I'm a real Novice to VB!

I have a text file, that my program is always adding to it, it's format is like so:

Line 1: UserName (For User 1)
Line 2: FilePath (For User 1)
Line 3: UserName (For User 2)
Line 4: FilePath (for User 2)

For Example:

Line 1: Tobyjug2222
Line 2: J:\Program name\bin\Debug\Accounts\Tobyjug2222
Line 3: Toby
Line 4: J:\Program name\bin\Debug\Accounts\Toby
...
...

My program automatically writes this to the file when an account is created, and there for I don't know what my Upper bound is, as It's always changing.

How can I write this to an array and read it as Line 1, and Line 2 (Or Line 3 and Line 4) are related?

I hope I've added all the information needed,

Thanks, Toby.

(P.S. This is related to my other thread : http://www.daniweb.com/software-development/vbnet/threads/398774/1709547#post1709547 )

Tobyjug2222
Newbie Poster
17 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

Does it REALLY need to be an array?
Would a List of classes or List of KeyValuePairs work?

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

Yes, Sure.. I thought that an Array would be easiest, but anything really.

Tobyjug2222
Newbie Poster
17 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

I know "array" sounds easier to use, but I find they require more management than the collection classes.
I know some of these other things look hard to use at first, but if you think about it: without them, you're doing all of the work they will do for you.

I would start out with something like a List of Key/Value pairs.
The Key (user name) is on the left and the Value (path) is on the right.
...and store those in a list. My list would be named like this:

Dim lst_kvp_s2sNamePath As New List(Of KeyValuePair(Of String, String))


I would then
1) Open the text file as a StreamReader
2) Loop through the file with TWO ReadLine calls per pass (one for name, one for the path)
3) Simultaneously add the key and value to the key/value pair and to the list with:

lst_kvp_s2sNamePath.Add(New KeyValuePair(Of String, String)(strName, strPath))


If later you need to see the contents of the list, you can loop through it with a For Each

Let me know if that's confusing.

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

If the Name-ID things are guaranteed to be unique, you can use a Dictionary(String, String).
That might make it less cryptic.
I used the KVP because it does not require the individual keys to be unique.

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

Yes - Each Username and Path will be Unique.

Tobyjug2222
Newbie Poster
17 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

I'm trying out your method now, I seem slightly lost, but I will see if I can understand it or not :)

Tobyjug2222
Newbie Poster
17 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

.see if this helps.

Imports System.IO
Public Class Form1
    Private myFile As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\test.txt"
    Private arFileLines() As String = Nothing

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        myCoolSub()
    End Sub
    Private Sub myCoolSub()
        If File.Exists(myFile) Then
            arFileLines = File.ReadAllLines(myFile)
            For i As Integer = 0 To arFileLines.Length - 1 Step +2 '// loop thru file.lines and skip reading every other line.
                MsgBox(arFileLines(i) & vbNewLine & arFileLines(i + 1))
            Next
        End If
    End Sub
End Class
codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 

Thanks a lot both of you, I used thines01's method, although thanks for the contribution codeorder :)

Tobyjug2222
Newbie Poster
17 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: