I am trying to save a listview item and 5 subitems to a txt file, I have tried one solution that was posted on here but i could not manipulate the code for all of my sub items can someone please help me out.

Private Sub SaveListItem(ByVal FileName As String, ByVal LI As ListViewItem)
        If String.IsNullOrEmpty(FileName) OrElse LI Is Nothing Then Exit Sub

        Dim fStream As System.IO.FileStream
        Dim sWriter As System.IO.StreamWriter

        Try
            fStream = New System.IO.FileStream(FileName, IO.FileMode.OpenOrCreate)
            sWriter = New System.IO.StreamWriter(fStream)

            For i = 0 To LI.SubItems.Count - 1
                sWriter.WriteLine(LI.SubItems(i).Text)
            Next

            sWriter.Close()
            fStream.Close()

        Catch ex As Exception

        Finally
            If fStream IsNot Nothing Then fStream.Dispose()
            If sWriter IsNot Nothing Then sWriter.Dispose()
        End Try
    End Sub

Edited 5 Years Ago by ~s.o.s~: added code tags, please earn to use them

It can't be the one you tried. I wrote it a little while ago.

Tell exactly what you want and what you mean by subposts and I'll send you the code.

It can't be the one you tried. I wrote it a little while ago.

Tell exactly what you want and what you mean by subposts and I'll send you the code.

Not the code that you wrote but the code that codeorder wrote, if you would have payed attention. Look at the link that codeorder put in the post and you will see what I need but with 5 subposts.

See if this helps.

Public Class Form1

    Private myCoolFile As String = "C:\test.txt" '// your File to Save/Load from.

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        '//------ Save File ------------------------------------------\\
        Dim myWriter As New IO.StreamWriter(myCoolFile)
        For Each myItem As ListViewItem In ListView1.Items '// loop thru all items in ListView.
            With myItem
                '// write Item & "#" & SubItems... "#" is used for when loading each line and splitting it into arrays.
                myWriter.WriteLine(myItem.Text & "#" & .SubItems(1).Text & "#" & .SubItems(2).Text & "#" _
                                   & .SubItems(3).Text & "#" & .SubItems(4).Text & "#" & .SubItems(5).Text)
            End With
        Next
        myWriter.Close()
    End Sub


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '// customize ListView.
        ListView1.View = View.Details
        With ListView1.Columns
            '// .Add(column name, column width)
            .Add("column 1", 75) : .Add("column 2", 75) : .Add("column 3", 75) : .Add("column 4", 75)
            .Add("column 5", 75) : .Add("column 6", 75)
        End With
        '//------ Load File ------------------------------------------\\
        If IO.File.Exists(myCoolFile) Then '// check if File exists.
            Dim myCoolFileLines() As String = IO.File.ReadAllLines(myCoolFile) '// load your File as a string array.
            For Each line As String In myCoolFileLines '// loop thru array list of File lines.
                Dim lineArray() As String = line.Split("#") '// separate by "#" character.
                Dim newItem As New ListViewItem(lineArray(0)) '// add text Item.
                With newItem.SubItems
                    .Add(lineArray(1)) '// add SubItem 1.
                    .Add(lineArray(2)) '// add SubItem 2.
                    .Add(lineArray(3)) '// add SubItem 3.
                    .Add(lineArray(4)) '// add SubItem 4.
                    .Add(lineArray(5)) '// add SubItem 5.
                End With
                ListView1.Items.Add(newItem) '// add Item to ListView.
            Next
        End If
    End Sub

    '//------ add Items/SubItems to ListView (testing purposes only) ------------------------------------------\\
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Static i As Integer = 0
        Dim newItem As New ListViewItem("item " & i) '// add text Item.
        With newItem.SubItems
            .Add("subitem 1 - " & i) '// add SubItem 1.
            .Add("subitem 2 - " & i) '// add SubItem 2.
            .Add("subitem 3 - " & i) '// add SubItem 3.
            .Add("subitem 4 - " & i) '// add SubItem 4.
            .Add("subitem 5 - " & i) '// add SubItem 5.
        End With
        ListView1.Items.Add(newItem) '// add Item to ListView.
        i += 1
    End Sub
End Class

Not the code that you wrote but the code that codeorder wrote, if you would have payed attention. Look at the link that codeorder put in the post and you will see what I need but with 5 subposts.

Don't need to pay attention. Don't care. Writing 5 items or "subposts" is a pretty trivial task. The code I gave you does it. Just need to do it 5 times.

This question has already been answered. Start a new discussion instead.