runtime: Environment.GetFolderPath(Environment.SpecialFolder.Personal) returns different path for Xamarin.Forms and for MAUI on Android
Description
During migration of an app from Xamarin.Forms was found weird behaviour:
Environment.GetFolderPath(Environment.SpecialFolder.Personal) returns another path on Android platform:
Xamarin.Forms: /data/user/0/<packageName>/files
MAUI: /data/user/0/<packageName>/files/Documents
It creates issue with an access to saved files during update process
Steps to Reproduce
- Create Core .net Standard project with class which returns
Environment.GetFolderPath(Environment.SpecialFolder.Personal); - Create Xamarin.Forms project and add reference to Core project.
- Call core-class from Xamarin.Forms somehow, check string
- Create MAUI project and add reference to Core project.
- Call core-class from MAUI somehow, check string
- Compare strings from both apps
Link to public reproduction project repository
https://github.com/yevgeny-sotnikov/MAUI-issues/tree/environment-folder-android-issue
Version with bug
8.0.7 SR2
Is this a regression from previous behavior?
Yes, this used to work in Xamarin.Forms
Last version that worked well
5.0.0.2012
Affected platforms
Android
Affected platform versions
Android 10 and upper
Did you find any workaround?
Remove last Documents folder at path
Relevant log output
No response
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 11
- Comments: 20 (11 by maintainers)
@ivanpovazan if you wanted to add a note here, and link to that
getfolderpath-unixpage:There is probably a similar page for Xamarin.iOS.
I have deleted the original message as I want to do some more testing, but it does appear to be so - I had some items in the app and I don’t see them anymore just an empty list. However as I have tried to “search” for the old database on various paths including Android native APIs it seems not to be present so it might have been somehow deleted during the process… I will test on another device soon to be sure.
Just to clarify:
Xamarin.Androidproject where you useEnvironment.GetFolderPath(Environment.SpecialFolder.Personal).NET8 Androidand replaced all references ofEnvironment.GetFolderPath(Environment.SpecialFolder.Personal)toEnvironment.GetFolderPath(Environment.SpecialFolder.UserProfile)but the old data is not pulled in?Even i have the same issue with local dabase where the app failed to get the data base path i have tried the above solution of using Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) but Application is failing to deploy on top of xamarin forms application.
It depends on which folder you would expect to retrieve. The following tables describe the difference between the behavior in previous releases (and the introduced breaking change in .NET8).
$HOMEdirectory, then as the documentation states you should be usingEnvironment.GetFolderPath(Environment.SpecialFolder.UserProfile)Documentsfolder, then you should keep usingEnvironment.GetFolderPath(Environment.SpecialFolder.Personal)Please let us know if you have any other question.
The macCatalyst version of this is https://github.com/dotnet/runtime/issues/100440. @yevgeny-sotnikov could you please check it - there we plan to change the behavior at least in some cases.
@jonathanpeppers - is this a dotnet/runtime issue?
@yevgeny-sotnikov the behaviour in
Xamarin.Formswas incorrect and it didn’t meet user expectations for Linux, macOS, and Android. In .NET8 preview 1 we corrected this and introduced this breaking change: https://learn.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/8.0/getfolderpath-unixTo get the desired behaviour, the recommended action is to use:
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)instead.@jonathanpeppers what would be the best place to include information about such required actions while migrating to MAUI?