Xamarin : Android : System.UnauthorizedAccessException: Access to the path is denied

Issue

So I’m trying to create a file and I’m getting System.UnauthorizedAccessException: Access to the path “/DownloadJitters” is denied. I’m not sure if it’s a permissions thing (I’ve tried a write to external storage in case but that didn’t work) or something else. Also I’m trying to figure out a good place to write these files as I would like them not to be easily found. Any ideas? Here’s the code as well :

public void favouriteList(MainActivity av, Ordering o, string favouriteName, string totalCost, JittersListView jlv)
    {
        //Checks Directory exists
        if (File.Exists(Android.OS.Environment.DirectoryDownloads + "/Jitters/FavouritesListAdded.txt")  false)
        {
            Directory.CreateDirectory(Android.OS.Environment.DirectoryDownloads + "Jitters/FavouriteList/");
            File.Create(Android.OS.Environment.DirectoryDownloads + "/Jitters/FavouritesListAdded.txt");
        }

        if (File.Exists(Android.OS.Environment.DirectoryDownloads + "Jitters/FavouriteList/" + favouriteName + ".txt")  false)
        {
            var fav  File.Create(Android.OS.Environment.DirectoryDownloads + "Jitters/FavouriteList/" + favouriteName + ".txt");
            fav.Close();
            string file  Android.OS.Environment.DirectoryDownloads + "Jitters/FavouriteList/" + favouriteName + ".txt";
            string added  null;
            int current  0;
            while (true)
            {
                if (current < jlv.Count)
                {
                    JittersListItem jli  jlv[current];
                    added + jli.Top + "|" + jli.Bottom + "|" + jli.itemPic + "|" + jli.itemDes + System.Environment.NewLine;
                    current++;
                }
                else
                {
                    break;
                }
            }
            File.AppendAllText(file, favouriteName + "|" + totalCost + added);
        }
        else
        {
            new AlertDialog.Builder(av)
                    .SetMessage("Please use a different name, this one has been taken.")
                    .Show();
        }
    }

Solution

Ok I Fixed it by changing the saving location to System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)

Don’t ask me why that worked when they need the same permissions but it did.

Answered By – Euan Hollidge

Leave a Comment