reading files from a directory
its showing me the file names with the directory included, i just want it to display the file name on each line without the directory??
12 Replies
Split the path by
\
and get just the last segment
That'll be the easiest wayhttps://learn.microsoft.com/en-us/dotnet/api/system.io.path.getfilename?view=net-8.0 will get just the file name
Angius
REPL Result: Success
Result: string
Compile: 387.446ms | Execution: 31.870ms | React with ❌ to remove this embed.
no need to implement the splitting yourself
Use DirectoryInfo and FileInfo types instead for this kind of thing.
i understand the use for 1 file
but i dont understand how i can use iterations to get all file name
s
in a directory
You get all the file paths and, in a loop, extract the file names from them
The code I showed does exactly that.
can you show me bro
i dont want to just copy paste this code
i dont understand
ive just been staring at this shit
for an hour
trying not to punch a wall
so you already have a loop with the absolute path to the file in
filePaths[i]
pick either Path.GetFileName or manually splitting and use that to get just the name
or rewrite the whole thing based on mtreit's example
so i got it to work
but it doesnt work like before
as i have no array
of file names
which is what i need
to access the file selected
files
is the array that stores full file paths
If you want to have a collection of just the file names, you can also use the loop for that
Create a new List<string>
that would store the names alone
And, in the loop, for each path you have, add just the filename to that list
Alternatively, you can use LINQ method .Select()