Member Avatar for grgrl

I would like to know how to take selected information from a listview and add them to a .dat file

This is what i got so far but it is to save into a text file
Sorry for any mistakes i am a noob

Private _filepath2 As String = Application.StartupPath & "\Teachers/Courses.dat"
Private Sub SaveToFile(LV As ListView, fpath As String)

    Dim swriter As New StreamWriter(fpath, True)
     Dim line As String
    With swriter
        For i As Integer = 0 To Listview1.Items.Count - 1
            swriter.Write(Listview1.Items(i).Text)
            For subItemIndex As Integer = 1 To Listview1.Items(i).SubItems.Count - 1
                swriter.Write(Listview1.Items(i).SubItems(subItemIndex))
            Next
            sw.Write(line.NewLine)

        Next
        sw.Close()
    End With
End Sub
and to call the function


  Private Sub ButtonUpdate_Click(sender As Object, e As EventArgs) Handles ButtonUpdate.Click
  SaveToFile(ListViewInformation, "Teachers/Courses.dat")
End Sub

As far as I know a .dat can hold any type of data.

Member Avatar for grgrl

Thx for the reply

i figured it out but i would like to save the data permanently in the file instead of everytime i run the program the file empties out

heres what i wrote
Private Sub ButtonAdd_Click(sender As Object, e As EventArgs) Handles ButtonAdd.Click

    Dim swriter As New StreamWriter(Application.StartupPath & "\TeachersCourses.dat")
    For i = 0 To ListViewInformation.Items.Count - 1
        swriter.WriteLine(ListViewInformation.Items.Item(i).Text & ":" & ListViewInformation.Items.Item(i).SubItems(1).Text)
    Next
    swriter.Close()

End Sub

try:
Dim swriter As New StreamWriter(Application.StartupPath & "\TeachersCourses.dat", append:=True)

Member Avatar for grgrl

thx

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.