[VB.NET]Text Processing Question RRS feed

  • Question

  • I have got a CheckedBox List that reads folder names under a specified path and allows users to select folders they want.

    When the user clicks the button, I want the application to find a config file named "xxx.cfg" in the folders selected by the user.

    In that cfg file there will be texts like this:








    I want to read the texts after every "name" and "icao", and write some texts like this with 

    <name=yyy0, icao=zzz0>

    <name=yyy1, icao=zzz1>


    So how can I make this work? Thanks!

    Monday, August 13, 2018 12:16 AM

All replies

  • Hi

    Looks like a straightforward task.

    Is this school work?

    Please show you code so far.

    Regards Les, Livingston, Scotland

    Monday, August 13, 2018 12:25 AM
  • What you are showing is a bit disjointed e.g.

    Are the entries like the first one or the second one? Seems the CheckedListBox is not relevant either, only the operation for reading information. Please try again with a better description.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, August 13, 2018 12:28 AM
  • There could be infinite texts in this format in the cfg file:










    And I want it to create texts like this by reading the cfg file:

    <name=tom, icao=jerry>

    <name=cat, icao=mouse>

    <name=android, icao=apple>

    Monday, August 13, 2018 3:41 AM
  • No it's not school work I just want to add this feature to my project but I don't really know a way to do this
    Monday, August 13, 2018 3:42 AM
  • What I mean by the CheckedListBox is basically,

    there will be a tree:







    and what the checkedlistbox does is allowing the user to choose which folder with the cfg file they want to process

    For example if the user chooses only Folder1, the application will only read and process the config file in Folder1. If Folder2 and 3 are chosen, it will process the files in 2 and 3.
    Monday, August 13, 2018 3:46 AM
  • Hi,

    Do you want this effect?


    Public Class Form1
        Dim arrlTemp As New ArrayList
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Using srObject As New System.IO.StreamReader(CheckedListBox1.SelectedItem.ToString & "\1.cfg")
                Dim strTemp = srObject.ReadLine
                With srObject
                    While Not strTemp Is Nothing
                        strTemp = .ReadLine
                    End While
                End With
            End Using
            For i = 1 To arrlTemp.Count - 1 Step 3
                ListBox1.Items.Add("<" & arrlTemp(i) & "," & arrlTemp(i + 1) & ">")
        End Sub
    End Class

    Best Regards,


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Monday, August 13, 2018 5:29 AM
  • Here's an example function.  This method assumes that the name and icao lines could be anywhere in the block and that there could be other key-value pairs mixed in.  This code will look for opening block elements (lines beginning with [) and then look for two lines that begin with name= and icao=.  Once both are found, it will add the two associated values and then look for the next [ line.  If the elements shown in your example are the only lines of the file (there are no other values in each block besides name and icao) and those elements are always in that same order, then you could use Alex's example instead.  This example is designed to handle files that have additional, unneeded lines.

    Public Function GetNameValuePairs(filePath As String) As IEnumerable(Of Tuple(Of String, String))
        Dim result As New List(Of Tuple(Of String, String))
        Using reader = New IO.StreamReader(filePath)
            Dim inBlock As Boolean = False
            Dim name As String = Nothing
            Dim icao As String = Nothing
            Do While Not reader.EndOfStream
                Dim line = reader.ReadLine
                If line.StartsWith("[") Then
                    inBlock = True
                ElseIf inBlock Then
                    If line.StartsWith("name=") Then
                        name = line.Substring(5)
                    ElseIf line.StartsWith("icao=") Then
                        icao = line.Substring(5)
                    End If
                    If name IsNot Nothing AndAlso icao IsNot Nothing Then
                        result.Add(New Tuple(Of String, String)(name, icao))
                        name = Nothing
                        icao = Nothing
                        inBlock = False
                    End If
                End If
        End Using
        Return result
    End Function

    Usage example:

    For Each pair In GetNameValuePairs("example1.cfg")
        RichTextBox1.AppendText($"<name={pair.Item1}, icao={pair.Item2}>{vbCrLf}")

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Monday, August 13, 2018 1:53 PM
  • Hi Reed, there is still a little problem with this, because there is a Checkedlistbox that allows users to choose multiple paths that contain the .cfg file, how can I make this able to process multiple files? Otherwise this works perfectly.
    Thursday, September 6, 2018 5:15 AM
  • Just loop over the checked list box items and call this method for each checked path.  Add the results to a list of tuples.

    Dim values As New List(Of Tuple(Of String, String))
    For Each entry In CheckedListBox1.CheckedItems

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Thursday, September 6, 2018 10:28 AM
  • Ok perfect, but is there a mistake here in this?

    Using Aircraftcfg As New System.IO.StreamReader(Form1.ControlSelect.CheckedListBox1.SelectedItem.ToString & "\aircraft.cfg")

    There was no warning when I entered this but when I started debugging, Visual Studio says there is a "System.NullReferenceException" error.

    Saturday, September 8, 2018 5:43 PM
  • That would indicate that at the point where this code executed, there wasn't anything selected in the list box.

    You should break that line of code up to first check to see if anything is selected, if so, get the string representation, and only then attempt to create the stream reader.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Saturday, September 8, 2018 7:52 PM
  • Hmm interesting, because there were actually things selected in the checkedlistbox
    Wednesday, September 12, 2018 7:42 PM