How to display the file name only vs the whole path

Easyrider43
Easyrider43 used Ask the Experts™
on
What I have displays the whole path to the file name and I want just the file name to appear in the list box, any ideas on how I could change this code to just display the file name and not the whole path to each file?? in stead of

c:\test\hello.php

I it just to show

hello.php

here is what i have
Public Sub user_directory_Load(ByVal lstFiles As System.Object, ByVal txtDirectory As System.Object) Handles MyBase.Load

        If Not Directory.Exists(dir) Then
            MessageBox.Show("Directory Does Not Exist")
            Exit Sub
        End If
        If Not Directory.Exists(fil) Then
            MessageBox.Show("Directory That holds the files Does Not Exist")
            Exit Sub
        End If
        For Each files In IO.Directory.GetFiles(fil)
            user_directory.Items.Add(files)
        Next
        user_directory.Items.AddRange(IO.Directory.GetFiles(fil))
        user_directory.Items.AddRange(IO.Directory.GetDirectories(dir))

    End Sub

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
if you are using vb.net no need to use filesystemobject
use this

 Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' make a reference to a directory
        Dim di As New IO.DirectoryInfo("c:\")
        Dim diar1 As IO.FileInfo() = di.GetFiles()
        Dim dra As IO.FileInfo

        'list the names of all files in the specified directory
        For Each dra In diar1
            ListBox1.Items.Add(dra)
        Next
    End Sub

Commented:
Try this
foreach (string f in System.IO.Directory.GetFiles("Your directory"))
            {
                Console.WriteLine(System.IO.Path.GetFileName(f));
            }

Commented:
The same will be for the directories, using System.IO.Directory.GetDirectories

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