skip to Main Content

I’m trying to save a backup file file in my .net maui app, but somehow I can’t manage to get the correct path for saving my files. So far the App is developped for Android only, but I want to keep it as generic as possible. I ended up finding a workaround, but I’m sure this isn’t the way to ahcieve this.

My code to save a file so far :

public void WriteAppDataFile()
{
    if (AppData.User != null)
    {
        try
        {
            string FileName = String.Format("backup_user{0}.json", AppData.User.Id);
            string History = AppData.User.History;

            // Workaround I found is to hardcode the directory path, but no guarantee for the path to be the same on all situations or on all devices.
            string SaveDirectory = "/storage/emulated/0/Android/data/com.mycompany.myapp/files";

            var path = Path.Combine(SaveDirectory, FileName);
            File.WriteAllText(path, History);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }
}

The other options I tried so far :

  • Option 1
#if ANDROID
SaveDirectory = Android.App.Application.Context.GetExternalFilesDir(Android.OS.Environment.DirectoryDocuments).AbsoluteFile.Path;
#endif

Returns "/storage/emulated/0/Android/data/com.mycompany.myapp/files/Documents".
Saves the file at the given location, but is working only on Android and I can’t find a way to get this path without an extra folder, here Documents.

  • Option 2
SaveDirectory = FileSystem.Current.AppDataDirectory;

Returns "/data/user/0/com.mycompany.myapp/files".
According to the documentation, it should give me the appdata path, no matter the device type, but when running I can’t find the folder in the phone storage. However it compiles and I get no exception raised.

So here’s my question : what’s the best way to get the correct path to save files, ideally without specifying the device type ? And why is the FileSystem directory nowhere to be found on the phone ?

2

Answers


  1. Chosen as BEST ANSWER

    Okay actually after looking arround and thanks to the guidances in comment, it turns out there is no way to get a general location for user-accessible file without having some platform-specific code. I was able to get access to my generic folder in Android using this code :

    #if ANDROID
    docsDirectory = Android.App.Application.Context.GetExternalFilesDir(null).AbsoluteFile.Path;
    #endif
    

    It needs some implementation for other platforms as well, but at least I can get access to the folder wihtout having to hard code it, and the file is saved properly !


  2. Well, you have

    FileSystem.Current.AppDataDirectory;

    returns "/data/user/0/com.mycompany.myapp/files"
    

    And is that not the same as:

    string SaveDirectory = "/storage/emulated/0/Android/data/com.mycompany.myapp/files";
    

    Quite sure both are resolving to the same folder, so hands down then .AppDataDirectory looks to be the best choice here.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search