how to get only the name of the file, not the whole path?

B

Ben

Hi,

i need the filename of files located in a directory.

I use this:
Directory.GetFiles(Server.MapPath("~/mydirectory/")
For Each fileName In fileEntries
name=fileName
next

but this guves the full path. Any way to get only the filename? Of course i
can use substring, but does it exist a function?
Thanks
Ben
 
L

Lloyd Sheen

Ben said:
Hi,

i need the filename of files located in a directory.

I use this:
Directory.GetFiles(Server.MapPath("~/mydirectory/")
For Each fileName In fileEntries
name=fileName
next

but this guves the full path. Any way to get only the filename? Of course
i can use substring, but does it exist a function?
Thanks
Ben

Check out the functions in the System.IO.Path namespace. This namespace has
all functions to manipulate file names etc.

LS
 
G

Guest

Hi,

i need the filename of files located in a directory.

I use this:
Directory.GetFiles(Server.MapPath("~/mydirectory/")
For Each fileName In fileEntries
name=fileName
next

but this guves the full path. Any way to get only the filename? Of course i
can use substring, but does it exist a function?
Thanks
Ben

' Create a reference to the current directory.
Dim di As New DirectoryInfo(Environment.CurrentDirectory)
' Create an array representing the files in the current
directory.
Dim fi As FileInfo() = di.GetFiles()
Console.WriteLine("The following files exist in the current
directory:")
' Print out the names of the files in the current directory.
Dim fiTemp As FileInfo
For Each fiTemp In fi
Console.WriteLine(fiTemp.Name)
Next fiTemp

http://msdn.microsoft.com/en-us/library/system.io.fileinfo.name.aspx

Hope this helps
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top