Link to home
Create AccountLog in
Avatar of techservices
techservices

asked on

Regex.Replace For All Items In ListBox

I am very new to VB.Net and am using Visual Studio 2005 to write my program.  I have a ListBox that contains a list of UNC paths and have two goals for this list:

1-  Remove the last directory segment from all UNC paths in the ListBox
2-  Remove all duplicate entries from the ListBox (haven't started working on this part yet)

Example ListBox data:
\\server1\user$\bob
\\server1\user$\dave
\\server2\home$\tom
\\server2\home$\mary

After processing the above listbox data, the result should be:
\\server1\user$
\\server2\home$

I already tested my Regex pattern using UNC paths in text boxes and verified that it works.  I think my problem has to do with data cast types (integer vs. string). I have found a few different solutions via Google, but none of them work:

'*************************
'*** Solution 1
'*************************
Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        For i As Integer = 0 To ListBox4.Items.Count - 1
            ListBox4.Items(i) = Regex.Replace(ListBox4.Items(i), "\\[a-z0-9]*.$", "")
        Next
    End Sub

'*************************
'*** Solution 2
'*************************
Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click

        For i As Integer = 0 To ListBox4.Items.Count - 1
            Dim Text As String = Convert.ToString(ListBox4.Items(i))
            Text = Regex.Replace(ListBox4.Items(i), "\\[a-z0-9]*.$", "")
            ListBox4.Items(i) = Text
        Next
End Sub
Avatar of techservices
techservices

ASKER

I should mention that I get no errors from the above code.  It just doesn't seem to do anything at all.
ASKER CERTIFIED SOLUTION
Avatar of techservices
techservices

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Fernando Soto
Hi techservices;

The code snippet below show do what you need.

Fernando
Imports System.IO

For Each pn In ListBox1.Items
    Dim pathName As String = Path.GetPathRoot(pn)
    If Not allPathRoots.Contains(pathName) Then
        allPathRoots.Add(pathName)
    End If
Next

Open in new window