- Home /
Return name of file only
I'm having an issue with reading only the name of the file that I want, not the entire directory.
Here are the relevant lines of code:
files = Directory.GetFiles(Application.dataPath + "/Levels/");
...
for (var file : String in files)
I want file to be the name of the file.
I'm sure the answer is a simple one, but I haven't been able to find one, despite searching and my own attempts.
Thanks
Answer by Rustam-Ganeyev · Sep 07, 2011 at 06:53 AM
use Path.GetFileName.
Use String.Split with a period as the separator, and select the first element in the string[] it returns. That should be the filename while the second element is the extension. See:
NO! Use system tools, do not build your own bycicles! Use Path.GetFileNameWithoutExtension
Sure, that would probably be better, since it allows you to have periods in the filename. While systems tool are easier where available, constructing your own in simple cases like this one isn't a complete catastrophy, though...
Your answer
Follow this Question
Related Questions
How to supress a # into a path string 1 Answer
Read dialog text from text file in javascript 0 Answers
String as Variable name 0 Answers
Can you load an AudioClip from the file system w/o using WWW? 0 Answers