1

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 ?

5
  • When saving files that should be accessible across app sessions but not necessarily by the user directly through file browsers, FileSystem.AppDataDirectory is typically the best choice. This directory is private to your app and is not accessible by the user or other apps, providing a secure location for sensitive data. However, if you need to save files that should be accessible by the user or shareable with other apps, you'll have to use platform-specific APIs or consider saving them to a public directory, which requires additional permissions, especially on Android. Commented Feb 22, 2024 at 18:28
  • For a cross-platform solution that keeps your code clean and maintainable, stick with FileSystem.AppDataDirectory for internal app data. If you require the files to be user-accessible, you'll need to implement additional logic to handle file sharing or exporting to user-accessible locations, which may involve platform-specific code or permissions. Commented Feb 22, 2024 at 18:29
  • According to this link FileSystem.AppDataDirectory returns the FilesDir of the current context, which are backed up using Auto Backup starting on API 23 and above Commented Feb 22, 2024 at 18:29
  • 1
    Ah I noticed the location was used for auto-backup, but didn't understand it was not user-accessible. The directory doesn't appear in file explorer, but the app itself can still access these files, isn't it ? Commented Feb 22, 2024 at 18:41
  • Well, try to write something in a file and check if you can read it back in Android Commented Feb 22, 2024 at 18:45

1 Answer 1

6

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 !

Sign up to request clarification or add additional context in comments.

2 Comments

I have just done the same thing and 100% agree with your conclusion and code. I'm going to upvote this - you should and can feel free to make this as an answer. I also deleted my post. And you are correct, this location is exposed as a external folder, and thus if you plug in a USB to your computer, you find the files inside (often the Android needs a re-boot before the files show, but your location does work.
Thanks for confirming ! Wanted to flag this as an answer, but it turns out you need to wait 2 days before tagging your own post as an answer ahah. I'm gonna update the answer with the extra code lines if I end up making this iOS-compatible as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.