Search Unity

Resolved Save a file to a user accessible folder

Discussion in 'macOS' started by jpom001, May 29, 2023.

  1. jpom001

    jpom001

    Joined:
    Dec 3, 2020
    Posts:
    69
    I want to be able to download a file from the persistent file path to somewhere where a user can access the file and attach it to an email. I think my issue here is more of a permissions issue, and Im having the same issue with Android, how do I enable this for my testflight users. Is there some permission I need to add to my info.plist file to enable this?

    1. Code (CSharp):
      1. public static void MoveFileToDownloadsFolder()
      2.     {
      3.         try {
      4.             string logfilename = "/logs.txt";
      5.             string sourcePath = Application.persistentDataPath + logfilename;
      6.             // Get the path to the Downloads folder based on the platform
      7.             string downloadsPath = "";
      8. #if UNITY_ANDROID && !UNITY_EDITOR
      9.         downloadsPath = "/storage/emulated/0/Download/" +  "game-logs";
      10. #elif UNITY_IOS && !UNITY_EDITOR
      11.         downloadsPath = Path.Combine(Application.persistentDataPath, "..", "Documents",  "game-logs");
      12. #else
      13.             downloadsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads", "game-logs");
      14. #endif
      15.             if (File.Exists(sourcePath))
      16.             {
      17.                 File.Copy(sourcePath, downloadsPath, true);
      18.                 Debug.Log("File moved to Downloads folder: " + downloadsPath);
      19.             }
      20.             else
      21.             {
      22.                 Debug.LogError("File not found: " + sourcePath);
      23.             }
      24.         } catch (Exception e)
      25.         {
      26.             Debug.Log("download logs fail execption message: " + e.Message);
      27.             Debug.Log("download logs fail execption: " + e);
      28.         }
      29.         }