You can use Path.GetFileName()
for (int i=0; i<file->Length; i++)
{
listBox1->Items->Add(Path::GetFileName(file[i]) );
}
Frage
I'm having a problem getting just the names of the .txt files inside a directory. I want to get all the names of all the .txt files inside my application's main directory and add them to a listbox. The code I'm using is working but it's returning the full directory while I only want the name of files. Here's the code:
String^ folder = Application::StartupPath ;
array<String^>^ file = Directory::GetFiles(folder, "*.txt");
for (int i=0; i<file->Length; i++)
{
listBox1->Items->Add(file[i] );
}
This is returning the full directory, for example: C:\programs\asd.txt
And I only want: asd.txt
How can I get just the name of the file?
Lösung
You can use Path.GetFileName()
for (int i=0; i<file->Length; i++)
{
listBox1->Items->Add(Path::GetFileName(file[i]) );
}
Andere Tipps
You can use the basename()
function.
e.g.
file = basename(Directory::GetFiles(folder, "*.txt");