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

  • Thread starter Thread starter Ben
  • Start date Start date
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
 
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
 
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 coursei
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
 
Back
Top