0

I long variable in vb.net which contains the following information,

Dim g As String = "$C:\Program Files\Cavaj Java Decompiler\cavaj.exe$C:\Users\Yoosuf\AppData\Local\Google\Chrome\Application\chrome.exe$C:\Program Files\DVD Maker\dvdmaker.exe$C:\Program Files\Adobe\Adobe Photoshop CS2\ImageReady.exe$C:\Program Files\Java\jre6\bin\javaws.exe$"

The $ symbol is used as a delimiter to separate each item from the other. I need to add the exe file name at the end of each path to a listbox. However the initial process of retrieving the variable to individual array elements is not working properly.

Dim strArr() As String = g.Split("$")  'This variable is empty
For count = 0 To strArr.Length - 1
Dim arr As String = strArr(count).Split("\")

Dim strval As String = ""
For i As Integer = 3 To arr.Length - 1
        strval = arr(i)
        Dim j As Integer = arr.Length - 1
        strval = arr(j)
        Dim result As String = strval.Substring(g.Length - 5)
        result = g.Substring(g.LastIndexOf("\") + 1)
        ListBox1.Items.Add(result)
 Next
Next
1
  • Works fine for me. strArr.Length = 7. I can't see any problem with your code Commented Mar 5, 2012 at 4:31

2 Answers 2

4

No need to do all this work. The System.IO.Path class has methods to do this for you. You want to use either System.IO.Path.GetFileName or System.IO.Path.GetFileNameWithoutExtension. Since you've already split all the file paths, just pass those paths to either of the aforementioned methods and add the result to your listbox.

Dim strArr() As String = g.Split("$")
For Each path As String In strArr
    ListBox1.Items.Add(System.IO.Path.GetFileName(path))
Next
Sign up to request clarification or add additional context in comments.

Comments

1

Please refer to the code below and the associated comments. Also I have comment out some code which I feel is not required based on what you want to do.

Dim strArr() As String = g.Split("$")  'This variable is empty
        For count = 0 To strArr.Length - 1
            Dim arr() As String = strArr(count).Split("\") ' Split returns an array

            Dim strval As String = ""
            For i As Integer = 3 To arr.Length - 1
                'strval = arr(i)
                Dim j As Integer = arr.Length - 1
                strval = arr(j)
                'Dim result As String = strval.Substring(g.Length - 5)
                'result = g.Substring(g.LastIndexOf("\") + 1)
                ListBox1.Items.Add(strval)
            Next
        Next

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.