Android: Directory.GetFiles() gives IOException: Not a directory

Hi all,

I have a strange behavior on my App that occures when I’m trying to read files from a directory.

I’m running this code to get all files in a directory. It workes fine on Windows, but gives me an error on Android

Translator.Language LoadPreferences()
    {
        Preferences preferences = null;

        //Test
        Debug.Log("UnityLog: Start Permission Check");
        if (CurrentPlatform == Platform.Mobile)
        {
            if (Permission.HasUserAuthorizedPermission(Permission.ExternalStorageWrite) == false)
            {
                //Test
                Debug.Log("UnityLog: Permission.ExternalStorageWrite missing");             
            }

            if (Permission.HasUserAuthorizedPermission(Permission.ExternalStorageRead) == false)
            {
                //Test
                Debug.Log("UnityLog: Permission.ExternalStorageRead missing");               
            }
        }

        //Test
        Debug.Log("UnityLog: PersistantDataPath: " + Application.persistentDataPath);
        if (Directory.Exists(Application.persistentDataPath + "/Preferences") == false)
        {
            Debug.Log("UnityLog: Directory Preferences existiert nicht mehr");
        }

        Debug.Log("UnityLog: Start Directory.GetFiles: Preferences");
        string[] fileEntries = Directory.GetFiles(Application.persistentDataPath + "/Preferences/");

        //Test!
        if (fileEntries == null)
        {
            Debug.Log("UnityLog: Instanzieren von fileEntries fehlgeschlagen");
        }

       return null;
    }

The directory is empty, but existes. I have checked it on the tablet. It is created by this code before if it would be missing:

void CheckDirectories()
    {
        if (Directory.Exists(Application.persistentDataPath + "/Preferences") == false)
        {
            Directory.CreateDirectory(Application.persistentDataPath + "/Preferences");
            Debug.Log("UnityLog: Directory Preferences existiert nicht!");
        }

        //Test!
        if (Directory.Exists(Application.persistentDataPath + "/Preferences") == false)
        {
            Debug.Log("UnityLog: Directory Preferences konnte nicht erstellt werden!");
        }
        else
        {
            Debug.Log("UnityLog: Directory Preferences erfolgreich erstellt!");
        }

        if (Directory.Exists(Application.persistentDataPath + "/Saves") == false)
        {
            Directory.CreateDirectory(Application.persistentDataPath + "/Saves");
        }

        if (Directory.Exists(Application.persistentDataPath + "/Saves/Files") == false)
        {
            Directory.CreateDirectory(Application.persistentDataPath + "/Saves/Files");
        }

    }

I added the logcat from AndroidStudio below.

Every help would be appreaciated! Sorry for some German texts in the code ^^

Could you try it without trailing slash in directory name?

Thanks for your reply!
I tried it out and still get the same error message tough.

Maybe try doing both Directory.Exists and File.Exists on it? Maybe there is a file with that name, but the check for directory did not specifically tested for that?

I added a check for File.Exists which returns false. I checked it in the explorer as well and there is no file in there.

To make sure that the directories are consistant and I check the correct ones in the explorer I added a file now by this code:

 Debug.Log("Create a file");
        File.Create(Application.persistentDataPath + "/Preferences/Testfile.txt");

        if (File.Exists(Application.persistentDataPath + "/Preferences/Testfile.txt"))
        {
            Debug.Log("File was created");
        }
        else
        {
            Debug.Log("File not created");
        }

Which is succesfully created and which I also can find in the explorer on the tablet.
So it seems for me as if the paths work just find. But maybe your first idea was right and it is some type of syntax error wiuth how I call the directory.

Any ideas?

Try using Path.CombinePath(Application.persistentDataPath, “Preferences/Testfile.txt”) instead of manually creating the path-string. Some OS’s act weird when they see a ‘/’ but are expecting a '', etc.

Thats really valuable advice in my oppinion, but sadly doesn’t change anything :frowning:

Could you print out the full path of

Application.persistentDataPath, "Preferences/Testfile.txt"

Try using Android Studio device explorer or adb to go that directory and see what files/directories are present.

I allready did that. I looked it up directly on the device and found that file is existing there.

Please print out the path.