0

Output

Input

I am trying to create a dropdown with all the files located in a specific path the problem is when I load the files the show in the dropdown with the full path as a name for it how I change this and just keep the file name show in the dropdown? How can I fix it?

void loadedfiles()
{
    string[] myload = getfilesname();
}

string[] getfilesname()
{
    string folderPath = Path.Combine(Application.persistentDataPath, foldername);
    string[] filePaths = Directory.GetFiles(folderPath, "*.txt");
    foreach (string file in filePaths)
    {
        mylist.Add(file);
        Debug.Log(file);
    }
    dropi.AddOptions(mylist);
    return filePaths;
}
Utkarsh Dubey
  • 703
  • 11
  • 31
Komar
  • 33
  • 5

1 Answers1

0

Youc can use Path.GetFileName(string path) (or also Path.GetFileNameWithoutExtension(string path)) to get only the filename part from a given path string.

string[] filePaths = Directory.GetFiles(folderPath, "*.txt");
foreach (string file in filePaths)
{
    var onlyFileName = Path.GetFileName(file);

    mylist.Add(onlyFileName);
    Debug.Log(onlyFileName);
}

As alternative but maybe not the best/cleanest solution

string[] filePaths = Directory.GetFiles(folderPath, "*.txt");
foreach (string file in filePaths)
{
    // split the path into all parts on the seperators (/ or \)
    var pathParts = file.Split(Path.DirectorySeparatorChar);

    // take only the last part which should be the filename
    var onlyFileName = pathParts[pathParts.Length - 1];

    mylist.Add(onlyFileName);
    Debug.Log(onlyFileName);
}
derHugo
  • 83,094
  • 9
  • 75
  • 115
  • thank you ites work i really thank you i just removed the convert from it and its work perfectly thank you again – Komar Mar 04 '19 at 11:36