C# Getting only the Name of the XML File and not the Directory of the XML File

Hi everyone, is there a way to get only the name of an xml file and not the directory of the file? I declared public string XMLfiles = Directory.GetFiles(Application.dataPath,“*.XML”); which does list the XML files but it doesn’t get the names of the files . In the elements it lists where the XML files are located within the project but I want the names only. Is there a way to do that?

When you have a string of the full path (which you get from Directory.GetFiles) you can use the .NET method System.IO.Path.GetFileName(fullPath) to get the file name plus extension.

// GetFileName('C:\mydir\myfile.ext') returns 'myfile.ext'
// GetFileName('C:\mydir\') returns ''

Use System.IO.Path.GetFileName after getting your string array.

public string[] XMLfiles = Directory.GetFiles(Application.dataPath,"*.XML");
foreach(string file in XMLfiles)
{
    print(Path.GetFileName(file));
}

Of course, you’ll need to modify that to do what you want with each file name.