File.Copy throws System.UnauthorizedAccessException but file is copied successful

SchulleberndSchullebernd Member ✭✭
edited August 2 in Xamarin.Forms

Hey,
maybe I found a bug or I do something wrong.

I have a Xamarin.Forms Application with a backup feature. The backup is nothing else then copying a *.db3 file from the applications folder to the external storage so that the user finally can connect its phone to the pc and save the backup files manully to anywhere he wants to.

The problem is: When I call File.Copy(..) I get a System.UnauthorizedAccessException but the copy job has finished successfully. All the Reading the contents from the external storage before and after the copy job is als ok. Android permissions are fully ok and granted successfully.

During the last weeks of development this feature worked perfectly. Yesterday evening I switched to VS2019 (due to some mono version mismatches in iOS).

            // get local database file path
            string sDatabaseFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), DATABASE_FOLDER, DATABASE_FILENMAE);

            // Create the backup directory
            Directory.CreateDirectory(targetPath);
            // Build backup database path
            string sBackupDBFolder = Path.Combine(targetPath, DATABASE_FOLDER);
            // Create the backup database directory
            Directory.CreateDirectory(sBackupDBFolder);
            // Build backup database filepath
            string sBackupDBFile = Path.Combine(sBackupDBFolder, DATABASE_FILENMAE);

            // Close possible active database connections
            if (database != null)
            {
                database.CloseDatabase();
                database = null;
            }

            // Copy database file from local storage to backup folder
            File.Copy(sDatabaseFile, sBackupDBFile, true); // Here I get the exception

Here is the code to query the write rights and get sure that the external storage is mounted.

if (MainActivity.ThisActivity.CheckSelfPermission(Manifest.Permission.WriteExternalStorage) == Permission.Granted)
{
    return Environment.MediaMounted.Equals(Environment.ExternalStorageState);
}

Can somebody help me with this? I was shortly before the beta release in play store but with this, no chance.

Edit - I use
Xamarin.Forms 4.1.0.618601
Xamarin.Android.. 28.0.0.1
The target API-Level is 28 Pie (also testet with 27 Oreo - same problem)

Answers

Sign In or Register to comment.