Hello,

I am stuck with this problem....

I have few folders that contains a filename as registration.txt --- note the folder names are different since as and when a new user will register a folder gets created with its name and then the text file with the details of the user....

I have to read the user id from the text files to the Listbox and then check if any user id has been repeated in the list box....(stuck here)

I have able to read the data but just not able to perform the check on the id...

Please someone help me in this

See the code below what I tried....

Imports System.IO

Public Class Form3
     Dim filename As String
     Private Sub Form3_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
          Dim di As New IO.DirectoryInfo("D:\UserDetails\") '----folder where all the user data is saved
          Dim diar1 As IO.DirectoryInfo() = di.GetDirectories()
          Dim dra As IO.DirectoryInfo
          Dim sr As StreamReader
          Dim line As String
          Dim Fields() As String
          Dim lines As String()
          For Each dra In diar1
               Dim filename1 As FileInfo = New FileInfo(di.ToString + dra.ToString + "\UserRegEntry.txt")
               If filename1.Exists Then
                    filename = di.ToString + dra.ToString + "\UserRegEntry.txt"
                    sr = New StreamReader(filename)
                    line = sr.ReadLine()
                    If line <> String.Empty Then
                         lines = IO.File.ReadAllLines(filename)
                         Fields = line.Split("*") '---* is the delimiter
                         ListBox1.Items.Add(dra.ToString + "----------" + Fields(0)) '---just to read the User ID
                    Else
                         ListBox3.Items.Add(dra.ToString)
                    End If
               Else
                    ListBox2.Items.Add(dra.ToString)
               End If

          Next
          Label1.Text = ListBox1.Items.Count.ToString + " UserRegEntry.txt files found"
          Label2.Text = ListBox2.Items.Count.ToString + " UserRegEntry.txt files not found"
          Label3.Text = ListBox3.Items.Count.ToString + " No data in txt file"
     End Sub
End Class

If you keeps the ID's in a list and set the list as a datasource for ListBox1, any changes made to the list are echoed in the listbox. This allows you to leverage the Find method of the list to see if an ID exists. I've modified your code to do this, however not knowing the exact format of the text file, I didn't feel confident in testing it fully. The basic techniques are sound though.

    Imports System.IO
    Public Class Form3
        Dim filename As String
        'Class level variable to hold the search string
        Dim IDToFind As String = ""
        Private Sub Form3_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Dim di As New IO.DirectoryInfo("D:\UserDetails\") '----folder where all the user data is saved
            Dim diar1 As IO.DirectoryInfo() = di.GetDirectories()
            Dim dra As IO.DirectoryInfo
            Dim sr As StreamReader
            Dim line As String
            Dim Fields() As String
            Dim lines As String()

            'List to hold the ID's
            Dim ID as List(Of String) = New List(Of String)

            'Set the list as datasource
            ListBox1.DataSource = ID

            For Each dra In diar1
                Dim filename1 As FileInfo = New FileInfo(di.ToString + dra.ToString + "\UserRegEntry.txt")
                If filename1.Exists Then
                    filename = di.ToString + dra.ToString + "\UserRegEntry.txt"
                    sr = New StreamReader(filename)
                    line = sr.ReadLine()
                    If line <> String.Empty Then
                        lines = IO.File.ReadAllLines(filename)
                        Fields = line.Split("*") '---* is the delimiter

                        'Set the search string to the next ID
                        IDToFind = Fields(0)

                        'If the ID isn't found add it to the list which will echo to Listbox1
                        If Not ID.Find(Addressof FindDups) Then
                            ID.Add(dra.ToString + "----------" + Fields(0)) '---just to read the User ID
                        End If
                    Else
                        ListBox3.Items.Add(dra.ToString)
                    End If
                Else
                    ListBox2.Items.Add(dra.ToString)
                End If
            Next
            Label1.Text = ListBox1.Items.Count.ToString + " UserRegEntry.txt files found"
            Label2.Text = ListBox2.Items.Count.ToString + " UserRegEntry.txt files not found"
            Label3.Text = ListBox3.Items.Count.ToString + " No data in txt file"
        End Sub

        'Search function.  The Find method enumerates though the list and sends each string to this function to compare it against the search string
        Private Function FindDups(ByVal Itm As String) As Boolean
            Return (Itm = IDToFind)
        End Function
    End Class
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.