Search code examples
.netvb.netienumerablegetdirectories

VB Show File/Folder Names


I am coding a programm that will run some batch files if they're selected by the user.

Each batch has its own folder in a specific "resources" folder.

I am looping through that folder to create checkboxes and labels. Everything's working fine, but I don't want the label to have the whole path - I just want to display the foldername.

CODE :

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Dim BatchFolder As IEnumerable = Directory.EnumerateFileSystemEntries(appPath & "/resources")
    Dim totalNumber As Integer = Directory.GetDirectories(appPath & "/resources").Length
    Dim i As Integer = 1

    For Each dir As String In BatchFolder
        Dim Label As New Label()
        Label.Name = "Lb_" & dir
        Label.Text = dir & ".bat"
        Label.AutoSize = True
        Label.Visible = True
        Label.Location = New Point(55, 4 + 25 * i)
        Dim CustomCheckbox As New Bunifu.Framework.UI.BunifuCheckbox()
        CustomCheckbox.Visible = True
        CustomCheckbox.Name = "CB_" & dir
        CustomCheckbox.Checked = False
        CustomCheckbox.Location = New Point(35, 25 * i)
        CustomCheckbox.CheckedOnColor = Color.FromArgb(12, 106, 255)
        Panel5.Controls.Add(Label)
        Panel5.Controls.Add(CustomCheckbox)
        i = i + 1
    Next
    Label21.Text = totalNumber
End Sub

This is how it looks:

enter image description here


Solution

  • You can do it like this :

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim BatchFolder As IEnumerable = Directory.EnumerateFileSystemEntries(appPath & "\resources")
        Dim totalNumber As Integer = Directory.GetDirectories(appPath & "\resources").Length
        Dim i As Integer = 1
    
        For Each dir As String In BatchFolder
            Dim Label As New Label()
            dir = dir.Substring(dir.LastIndexOf("\") + 1)
            Label.Name = "Lb_" & dir
            Label.Text = dir & ".bat"
            Label.AutoSize = True
            Label.Visible = True
            Label.Location = New Point(55, 4 + 25 * i)
            Dim CustomCheckbox As New Bunifu.Framework.UI.BunifuCheckbox()
            CustomCheckbox.Visible = True
            CustomCheckbox.Name = "CB_" & dir
            CustomCheckbox.Checked = False
            CustomCheckbox.Location = New Point(35, 25 * i)
            CustomCheckbox.CheckedOnColor = Color.FromArgb(12, 106, 255)
            Panel5.Controls.Add(Label)
            Panel5.Controls.Add(CustomCheckbox)
            i = i + 1
        Next
        Label21.Text = totalNumber
    End Sub
    

    Hope it will help you :)