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

  1. Create Core .net Standard project with class which returns Environment.GetFolderPath(Environment.SpecialFolder.Personal);
  2. Create Xamarin.Forms project and add reference to Core project.
  3. Call core-class from Xamarin.Forms somehow, check string
  4. Create MAUI project and add reference to Core project.
  5. Call core-class from MAUI somehow, check string
  6. 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)

Most upvoted comments

@ivanpovazan if you wanted to add a note here, and link to that getfolderpath-unix page:

There is probably a similar page for Xamarin.iOS.

Just to clarify:

* you have a `Xamarin.Android` project where you use `Environment.GetFolderPath(Environment.SpecialFolder.Personal)`

* you upgraded the project to `.NET8 Android` and replaced all references of  `Environment.GetFolderPath(Environment.SpecialFolder.Personal)` to `Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)` but the old data is not pulled in?

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.

I can confirm that this doesn’t work well instead of .Personal. While it makes the app not to crash it doesn’t pull old data, as far as I can tell.

I am migrating from Xamarin.Android to .NET 8 Android (or whatever it is called).

Just to clarify:

  • you have a Xamarin.Android project where you use Environment.GetFolderPath(Environment.SpecialFolder.Personal)
  • you upgraded the project to .NET8 Android and replaced all references of Environment.GetFolderPath(Environment.SpecialFolder.Personal) to Environment.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.

Will it keep same folder path on iOS and mac (same as at xamarin)? I don’t want to change current folder of files for all platforms

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).

.NET 7 Android iOS MacOS
Environment.GetFolderPath(Environment.SpecialFolder.Personal) /data/user/0/app_name/files /var/mobile/Containers/Data/Application/app_guid/Documents /Users/username/
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) /data/user/0/app_name/files /var/mobile/Containers/Data/Application/app_guid /Users/username/
.NET 8
Environment.GetFolderPath(Environment.SpecialFolder.Personal) /data/user/0/app_name/files/Documents /var/mobile/Containers/Data/Application/app_guid/Documents /Users/username/Documents
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) /data/user/0/app_name/files /var/mobile/Containers/Data/Application/app_guid /Users/username/
  • If you expect to retrieve the $HOME directory, then as the documentation states you should be using Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
    • NOTE: Please note that $HOME on iOS devices is not writable
  • If you expect to retrieve the Documents folder, then you should keep using Environment.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.Forms was 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-unix

To 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?