Hi all,

Going mad not being able to figure this out.

I have an application that uses the following code to write a number of user inputed variables to a text file on a seperate line per variable..

What I can figure out despite hours of googling, is how to reverse the process, and load the data from the text file to re-populate the variables.

Private Sub CmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdSave.Click
        'Check if file exists and warn of overwriting.
        Dim Response
        If File.Exists("c:\EveOreData.csv") Then
            Response = MsgBox("Overwrite Existing Save File?", MsgBoxStyle.YesNo, "Overwrite?")
            If Response = vbNo Then
                Exit Sub
            End If
        End If

        Dim MiningSkill, ExhumerSkill, AstrogeologySkill, MiningBargeSkill, IceHarvesting, MiningDroneOperation, DroneInterfacing As Integer
        Dim MyCSVWriter As System.IO.StreamWriter
        Dim MyFileName As String
        Dim MyNewLine As String

        'get data to save
        MiningSkill = CboMining.Text
        ExhumerSkill = CboExhumers.Text
        AstrogeologySkill = CboAstrogeology.Text
        MiningBargeSkill = CboMiningbarge.Text
        IceHarvesting = CboIceharvesting.Text
        MiningDroneOperation = CboMDOS.Text
        DroneInterfacing = CboDIS.Text
        'Create and write data
        MyFileName = "c:\EveOreData.csv"
    
        MyCSVWriter = My.Computer.FileSystem.OpenTextFileWriter(MyFileName, False)
        'Write each variable to a new line
        MyNewLine = MiningSkill
        MyCSVWriter.WriteLine(MyNewLine)
        MyNewLine = ExhumerSkill
        MyCSVWriter.WriteLine(MyNewLine)
        MyNewLine = AstrogeologySkill
        MyCSVWriter.WriteLine(MyNewLine)
        MyNewLine = MiningBargeSkill
        MyCSVWriter.WriteLine(MyNewLine)
        MyNewLine = IceHarvesting
        MyCSVWriter.WriteLine(MyNewLine)
        MyNewLine = MiningDroneOperation
        MyCSVWriter.WriteLine(MyNewLine)
        MyNewLine = DroneInterfacing
        MyCSVWriter.WriteLine(MyNewLine)
        MyCSVWriter.Close()
        'Give Save Confirmation
        Dim msg = "Data saved to File - C:\EveOreData.csv"
        Dim title = "Data Saved"
        Dim style = MsgBoxStyle.OkOnly
        MsgBox(msg, style, title)
    End Sub

If anyone can point me in teh right direction I'd be very grateful !!!!

Thanks in advance,

Peter

Been trying

Private Sub CmdLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdLoad.Click

        Dim MyFileName As String
        Dim Z As Integer
        Dim Ary(0) As String
        MyFileName = "C:\EveOreData.csv"

        Dim sr As New System.IO.StreamReader(MyFileName)
        Dim placeholder As Integer = 0
        Do While sr.Peek <> -1
            Ary(placeholder) = sr.ReadLine
            placeholder += 1
        Loop
        CboMining.Text = Ary(0)
        CboExhumers.Text = Ary(1)
        CboAstrogeology.Text = Ary(2)
        CboMiningbarge.Text = Ary(3)
        CboIceharvesting.Text = Ary(4)
        CboMDOS.Text = Ary(5)
        CboDIS.Text = Ary(6)
    End Sub

but get a "Index was outside the bounds of the array." error at the

Ary(placeholder) = sr.ReadLine

Line ??

change
Dim Ary(0) As String
to
Dim Ary(6) As String

Dim myFile As String = "c:\EveOreData.csv"
        If IO.File.Exists(myFile) Then
            Dim readLines() As String = IO.File.ReadAllLines(myFile)
            TextBox1.Text = readLines(0)
            TextBox2.Text = readLines(1)
            TextBox3.Text = readLines(2)
            TextBox4.Text = readLines(3)
            TextBox5.Text = readLines(4)
            TextBox6.Text = readLines(5)
            TextBox7.Text = readLines(6)
        End If
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.