Jimmu John Jimmu John - 1 year ago 112
Vb.net Question

Ouput data in a 1 dimensional array of string

I have a 1 dimensional array string that has maybe the most 5 known items in it. I want to output each however many may be in the array string to a label on my form in the format like "I have a total of item1, item2, item3, item4, item5 in my array". Here's how my array looks like:

Dim dirs As String() = Directory.GetFiles(C:\, "*.txt")

For each name in dirs
mylabel.text = name >>> need each item to be here. Only ouputing last item.

Answer Source

Path.GetFileName can be used to get just the file name from a full file path, and String.Join can turn the array into a comma separated string. Combining both with the LINQ Select method would replace your For Each loop with:

mylabel.Text = "I have a total of " &
    String.Join(", ", dirs.Select(Function(x) Path.GetFileName(x))) &
    " in my array"

There is also Path.GetFileNameWithoutExtension if you don't want the file extension in the output.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download