Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

Discussion in 'Android' started by Weltenschmerz, Jan 7, 2023.

  1. Weltenschmerz

    Weltenschmerz

    Joined:
    Jun 13, 2017
    Posts:
    9
    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

    Code (CSharp):
    1. Translator.Language LoadPreferences()
    2.     {
    3.         Preferences preferences = null;
    4.  
    5.         //Test
    6.         Debug.Log("UnityLog: Start Permission Check");
    7.         if (CurrentPlatform == Platform.Mobile)
    8.         {
    9.             if (Permission.HasUserAuthorizedPermission(Permission.ExternalStorageWrite) == false)
    10.             {
    11.                 //Test
    12.                 Debug.Log("UnityLog: Permission.ExternalStorageWrite missing");            
    13.             }
    14.  
    15.             if (Permission.HasUserAuthorizedPermission(Permission.ExternalStorageRead) == false)
    16.             {
    17.                 //Test
    18.                 Debug.Log("UnityLog: Permission.ExternalStorageRead missing");              
    19.             }
    20.         }
    21.  
    22.         //Test
    23.         Debug.Log("UnityLog: PersistantDataPath: " + Application.persistentDataPath);
    24.         if (Directory.Exists(Application.persistentDataPath + "/Preferences") == false)
    25.         {
    26.             Debug.Log("UnityLog: Directory Preferences existiert nicht mehr");
    27.         }
    28.  
    29.         Debug.Log("UnityLog: Start Directory.GetFiles: Preferences");
    30.         string[] fileEntries = Directory.GetFiles(Application.persistentDataPath + "/Preferences/");
    31.  
    32.         //Test!
    33.         if (fileEntries == null)
    34.         {
    35.             Debug.Log("UnityLog: Instanzieren von fileEntries fehlgeschlagen");
    36.         }
    37.  
    38.        return null;
    39.     }
    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:
    Code (CSharp):
    1. void CheckDirectories()
    2.     {
    3.         if (Directory.Exists(Application.persistentDataPath + "/Preferences") == false)
    4.         {
    5.             Directory.CreateDirectory(Application.persistentDataPath + "/Preferences");
    6.             Debug.Log("UnityLog: Directory Preferences existiert nicht!");
    7.         }
    8.  
    9.         //Test!
    10.         if (Directory.Exists(Application.persistentDataPath + "/Preferences") == false)
    11.         {
    12.             Debug.Log("UnityLog: Directory Preferences konnte nicht erstellt werden!");
    13.         }
    14.         else
    15.         {
    16.             Debug.Log("UnityLog: Directory Preferences erfolgreich erstellt!");
    17.         }
    18.  
    19.         if (Directory.Exists(Application.persistentDataPath + "/Saves") == false)
    20.         {
    21.             Directory.CreateDirectory(Application.persistentDataPath + "/Saves");
    22.         }
    23.  
    24.         if (Directory.Exists(Application.persistentDataPath + "/Saves/Files") == false)
    25.         {
    26.             Directory.CreateDirectory(Application.persistentDataPath + "/Saves/Files");
    27.         }
    28.  
    29.     }
    I added the logcat from AndroidStudio below.

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

    Attached Files:

  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    Could you try it without trailing slash in directory name?
     
  3. Weltenschmerz

    Weltenschmerz

    Joined:
    Jun 13, 2017
    Posts:
    9
    Thanks for your reply!
    I tried it out and still get the same error message tough.
     
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    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?
     
  5. Weltenschmerz

    Weltenschmerz

    Joined:
    Jun 13, 2017
    Posts:
    9
    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:

    Code (CSharp):
    1.  Debug.Log("Create a file");
    2.         File.Create(Application.persistentDataPath + "/Preferences/Testfile.txt");
    3.  
    4.         if (File.Exists(Application.persistentDataPath + "/Preferences/Testfile.txt"))
    5.         {
    6.             Debug.Log("File was created");
    7.         }
    8.         else
    9.         {
    10.             Debug.Log("File not created");
    11.         }
    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?
     
  6. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    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.
     
  7. Weltenschmerz

    Weltenschmerz

    Joined:
    Jun 13, 2017
    Posts:
    9
    Thats really valuable advice in my oppinion, but sadly doesn't change anything :(
     
  8. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,875
    Could you print out the full path of
    Code (CSharp):
    1. Application.persistentDataPath, "Preferences/Testfile.txt"
    Try using Android Studio device explorer or adb to go that directory and see what files/directories are present.
     
  9. Weltenschmerz

    Weltenschmerz

    Joined:
    Jun 13, 2017
    Posts:
    9
    I allready did that. I looked it up directly on the device and found that file is existing there.
     
  10. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,875
    Please print out the path.