System.IndexOutOfRangeException: 'Index was outside the bounds of the array.' error in vb.net

Hakan
Hakan used Ask the Experts™
on
Hi, i'm using below code to search some files in my directory with specific words. But there's a problem with it. It gives most of the time an error but for somewords it's ok.

Any help would be grateful, thank you.


    Private Sub cmdSearch_Click(sender As Object, e As EventArgs) Handles cmdSearch.Click
        If txtFilter.Text.Length > 4 Then
            Dim files As IEnumerable(Of String) = Directory.EnumerateFiles(txtDirectory.Text, "*.*", SearchOption.AllDirectories).Where(Function(s) s.Contains(txtFilter.Text) AndAlso (s.EndsWith("spck", StringComparison.OrdinalIgnoreCase) Or s.EndsWith(".buspck", StringComparison.OrdinalIgnoreCase)))
            dgvResult.Rows.Clear()
            lblWarning.Text = String.Empty
            If files.Count > 0 Then
                lblWarning.Text = files.Count & " files found!"
                For Each item In files
                    Dim fileArr() As String = Path.GetFileNameWithoutExtension(item).Split(New String() {"__"}, StringSplitOptions.None)
                    Dim typeArr() As String = fileArr(0).Split(New String() {"_"}, StringSplitOptions.None)
                    dgvResult.Rows.Add(typeArr(0), typeArr(1), fileArr(1), fileArr(2), fileArr(3), fileArr(4))
                Next
            Else
                lblWarning.Text = "No file found!"
            End If
        Else
            MsgBox("You should enter minimum 5 characters!")
        End If

    End Sub

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior .Net Consultant
Top Expert 2016
Commented:
on which line exactly? I suspect this one. Chances are that you don't have 4 double underscores in your fileArr.
dgvResult.Rows.Add(typeArr(0), typeArr(1), fileArr(1), fileArr(2), fileArr(3), fileArr(4))

Open in new window

Author

Commented:
Thank you Mr. Moreau i found problem as you said it causing because of that.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial