/storage/emulated/0/BatchFiles" System.UnauthorizedAccessException: 'Access to the path '/storage/emulated/0/BatchFiles' is denied.

Lou, Kecheng 60 Reputation points
2023-03-28T18:04:36.1833333+00:00

I get the following error when try to create a file in external storage in a android Xamarin in Visual studio 2022, android version is 12.

/storage/emulated/0/BatchFiles"

System.UnauthorizedAccessException: 'Access to the path '/storage/emulated/0/BatchFiles' is denied.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,293 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,367 questions
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 35,291 Reputation points Microsoft Vendor
    2023-03-29T02:03:12.69+00:00

    Hello,

    The reason for this problem is that Google has restricted the program's access to external storage in Android 11 and above, please refer to Storage updates in Android 11.

    With this update, your access to external access is limited to the Photo & Media folders, and you can't change the external storage yourself.

    In addition to this, you need to request storage permissions at runtime, see the following documentation and examples.

    var status = Permissions.RequestAsync<Permissions.StorageWrite>();

    For Android 11 and above file management, it is more recommended to store files in the internal folder of the program, which can increase security, please refer to Xamarin.Essentials: File System Helpers.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    3 people found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Lou, Kecheng 60 Reputation points
    2023-04-04T14:40:03.55+00:00

    Thanks Yonglun, it works. I do appreciate your help!

    0 comments No comments

  2. Lou, Kecheng 60 Reputation points
    2023-06-27T13:37:22.8266667+00:00

    Hi Yonglun,

    I understood it. Is it possible to send me a link of your team, I forgot how to submit a question to your team exclusively.

    thanks

    Kevin

    0 comments No comments