Find all files in folder and return their path or filename

Hey there!

Im on my way doing an Saving System,
im searching for a way to get all my .sav files from a directory

if i know the file name, i do it like so:

        private string GetPathFromSaveFile(string saveFile)
        {
            return Path.Combine(Application.persistentDataPath, saveFile + ".sav");
        }

but how actualy can i get the files by only knowing the path?

Ok nvm, found it 4mins later x)

for anyone searching the solution:

    void LoadSavedGames()
    {
        if (Directory.Exists(Application.persistentDataPath))
        {
            string worldsFolder = Application.persistentDataPath;

            DirectoryInfo d = new DirectoryInfo(worldsFolder);
            foreach (var file in d.GetFiles("*.sav"))
            {
                Debug.Log(file);
            }
        }
        else
        {
            File.Create(Application.persistentDataPath);
            return;
        }
    }
6 Likes

thanks for sharing I found this answer in about 30 seconds…

1 Like

I think this part is wrong:

        else
        {
            File.Create(Application.persistentDataPath);
            return;
        }

I think you need a different method to create a folder, but maybe it doesn’t matter with persistentDataPath. I’m sure since this folder is automatically created by unity now most people haven’t noticed this mistake, but you should do it this way instead:

        else
        {
            Directory.CreateDirectory(Application.persistentDataPath);
            return;
        }
2 Likes