How to just display the file name???

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
'Display Files in user directory'
    Public Sub user_directory_Load(ByVal lstFiles As System.Object, ByVal txtDirectory As System.Object) Handles MyBase.Load
        Dim fil As String = "c:\Test\"
        Dim dir As String = "c:\Test\"
        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 holes the files Does Not Exist")
            Exit Sub
        End If
        user_directory.Items.AddRange(IO.Directory.GetFiles(fil))
        user_directory.Items.AddRange(IO.Directory.GetDirectories(dir))

    End Sub

Open in new window

LVL 1
Easyrider43Asked:
Who is Participating?
 
Pratima PharandeCommented:
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
0
 
kodok_gorengCommented:
Imports System.IO
   
    Dim strFileSize As String = ""
    Dim di As New IO.DirectoryInfo("C:\temp")
    Dim aryFi As IO.FileInfo() = di.GetFiles("*.txt")
    Dim fi As IO.FileInfo

    For Each fi In aryFi
        strFileSize = (Math.Round(fi.Length / 1024)).ToString()
        Console.WriteLine("File Name: {0}", fi.Name)
        Console.WriteLine("File Full Name: {0}", fi.FullName)
        Console.WriteLine("File Size (KB): {0}", strFileSize )
        Console.WriteLine("File Extension: {0}", fi.Extension)
        Console.WriteLine("Last Accessed: {0}", fi.LastAccessTime)
        Console.WriteLine("Read Only: {0}", (fi.Attributes.ReadOnly = True).ToString)
    Next
0
 
X_layerCommented:
Try this:
'Display Files in user directory'
    Public Sub user_directory_Load(ByVal lstFiles As System.Object, ByVal txtDirectory As System.Object) Handles MyBase.Load
        Dim fil As String = "c:\Test\"
        Dim dir As String = "c:\Test\"
        Dim FSO
        Set FSO = CreateObject("Scripting.FileSystemObject")
        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 holes the files Does Not Exist")
            Exit Sub
        End If
        user_directory.Items.AddRange(FSO.GetFileName(IO.Directory.GetFiles(fil)))
        user_directory.Items.AddRange(IO.Directory.GetDirectories(dir))

    End Sub

Open in new window

This is untested code!
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Pratima PharandeCommented:
you need to use for loop to get file names


Instade of this

 user_directory.Items.AddRange(IO.Directory.GetFiles(fil))

try something like this
For each folderIdx In IO.Directory.GetFiles(fil)    
user_directory.Items.Add(folderIdx.Name)
 Next
0
 
Easyrider43Author Commented:
Hey Pratima --

it tells me that "Name" is not a member of the string??

why does it tell me this??
0
 
Pratima PharandeCommented:
0
 
Easyrider43Author Commented:
I wonder if its cause im using visual studio 2011??
0
 
Easyrider43Author Commented:
Pratima That worked!! except now when I click them to open I get an error saying


Overload resolution failed because no Public 'Start' can be called with these arguments:
    'Public Shared Function Start(fileName As String) As System.Diagnostics.Process':
        Argument matching parameter 'fileName' cannot convert from 'DirectoryInfo' to 'String'.
    'Public Shared Function Start(startInfo As System.Diagnostics.ProcessStartInfo) As System.Diagnostics.Process':
        Argument matching parameter 'startInfo' cannot convert from 'DirectoryInfo' to 'ProcessStartInfo'.

Open in new window

0
 
Easyrider43Author Commented:
Here is my open page code


Public Sub user_directory_Load(ByVal lstFiles As System.Object, ByVal txtDirectory As System.Object) Handles MyBase.Load
        Dim di As New IO.DirectoryInfo("\\secure\htdocs\onlineoffice\")
        Dim diar1 As IO.FileInfo() = di.GetFiles()
        Dim diar2 As IO.DirectoryInfo() = di.GetDirectories()
        Dim dra As IO.FileInfo
        Dim dir As String = "\\secure\htdocs\onlineoffice\"
        'list the names of all files in the specified directory
        
        If Not Directory.Exists(dir) Then
            MessageBox.Show("Directory That holds the files Does Not Exist", dir)
            Exit Sub
        End If
        For Each dra In diar1
            user_directory.Items.Add(dra)
        Next
        For Each dra2 In diar2
            user_directory.Items.Add(dra2)
        Next

    End Sub

    'Open the file when clicked'
    Private Sub user_directory_MouseDoubleClicked(ByVal sender As System.Object, ByVal e As System.Object) Handles user_directory.MouseDoubleClick
        Process.Start(user_directory.SelectedItem)
    End Sub

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.