Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Directory.GetFiles returning entire path, I only want the filename?

This is the code I have set up to scan a directory of files:

Dim fileArray() As String
fileArray = Directory.GetFiles(System.AppDomain.CurrentDomain.BaseDirectory & "help\")

And it successfully gets all files in the directory, but it gets their absolute paths aswell. For example, one of the entries in fileArray() is:

F:\Project\Project\bin\x86\Debug\help\book_troubleshoot.html

And I want it to just be:

book_troubleshoot.html

Is there a way to do this without parsing through all the array entries to trim off the path?

Thanks.

like image 271
blerh Avatar asked Apr 26 '10 23:04

blerh


People also ask

How can I get only the filename?

Normally find command will retrieve the filename and its path as one string. If you want to display only the filename, you can use basename command. find infa/bdm/server/source/path -type f -iname "source_fname_*. txt"

Does Directory GetFiles return full path?

GetFiles. This returns the file names in a folder. It returns a string array—this contains the full paths of all the files contained inside the specified directory.

Does full path include filename?

Paths include the root, the filename, or both. That is, paths can be formed by adding either the root, filename, or both, to a directory.

How can I find the filename without path?

Use the substring() method to get the filename without the path, e.g. fullPath. substring(fullPath. lastIndexOf('/') + 1) . The substring method will return a new string containing only the characters after the last slash.


1 Answers

string filename= System.IO.Path.GetFileName(fullpathname);
like image 147
renick Avatar answered Nov 03 '22 00:11

renick