Undesired behaviour of FileSystem.Current.AppDataDirectory
See original GitHub issueDescription
When you use FileSystem.Current.AppDataDirectory
it outputs a location that according to the documentation is
…the app’s top-level directory for any files that aren’t user data files. These files are backed up with the operating system syncing framework.
But the output of this on different platforms leads to confusing results.
OS | AppDataDirectory |
---|---|
iOS (simulator) | /Users/bradmoore/Library/Developer/CoreSimulator/Devices/7195125F-89A0-40B7-A8DB-1B343B2AF596/data/Containers/Data/Application/B6A225B4-55DE-447C-A407-6CFA3D118553/Library/ |
macOS | /Users/beeradmoore/Library/ |
Android | /data/user/0/com.beeradmoore.myAppName/files/ |
Windows | C:\Users\beeradmoore\AppData\Local\Packages\52ed336e-9df8-43e1-9c04-74392cd48f3d-218743784238_fids9vfx8fwe\LocalState\ |
I believe that using AppDataDirectory
should give a directory specific to your application. However on macOS it drops you into the root folder of where many applications place files. RIP my ~/Library/ folder which is now a mess on account of not even being able to debug basic strings anymore.
I would propose that for macOS PlatformAppDataDirectory created a folder with application name in either ~/User/Library/<application_name>/
or ~/Library/Application Support/<application_name>/
to give consistent behaviour across all platforms (application_name could also be bundle_id)
Additionally I don’t know if these files (possibly those Windows) are backed up by the OS to any cloud.
Steps to Reproduce
Create a MAUI app and run this on different platforms.
System.Diagnostics.Debug.WriteLine(FileSystem.Current.AppDataDirectory");
Version with bug
6.0 (current)
Last version that worked well
Unknown/Other
Affected platforms
macOS
Affected platform versions
macOS 12.4
Did you find any workaround?
Custom handle file paths when building MAUI app for macOS.
Relevant log output
No response
Issue Analytics
- State:
- Created a year ago
- Reactions:3
- Comments:5 (3 by maintainers)
Top GitHub Comments
@beeradmoore thanks for adding the info here, it’s a good explanation of this behaviour.
I have noticed the same behaviour with my Maui Mac Catalyst app since I’ve added a SQLite database and was looking for the file location so I could open it up. The way things currently operate I don’t think is feasible and it is absolutely opening Maui developers up to utter catastrophe. I’ve worked on Xamarin apps that recursivly deletes app sandbox directories, the damage from an XF to Maui migration could be catastrophic…
With regards to sandboxing by default, this is absolutely a startup template change Maui should introduce. The File > New > macOS route on Xcode enables sandboxing by default:
This would also make the platform experiences consistent because of the big 4 platforms (ios, android, mac, windows), mac is the only platform NOT to run in a sandbox by default.
Pros:
~/Library
folderCons:
An update on this. If you create a file
Platforms\MacCatalyst\Entitlements.plist
with the contents,then
FileSystem.Current.AppDataDirectory
will actually be/Users/beeradmoore/Library/Containers/<bundle_id>/Data/Library
which is more ideal.This is due to the app not running in a sandbox by default. As far as I am aware macOS apps published to the AppStore need to have sandbox mode enabled (although distributing with the AppStore isn’t the only way to release apps)
Additionally with Entitlements.plist existing does not behave correctly in debug mode as the codesign does not do its thing with entitlements when building for debug, only release.
I am unsure what the correct course of action should be. I don’t think people should be forced to run as a sandbox by default but there should be more clear as its very easy to kill your computer (eg. your app might have a clean up mode that deletes everything in FileSystem.Current.AppDataDirectory).
Also unsure if apps have any business adding data in to the Containers directory if they are not sandboxed. If that is ok then on macOS if not in a sandbox this call should return
so then at least there is consistent and not dangerous path.