Xamarin:Android:System.UnauthorizedAccessException:对路径的访问被拒绝 [英] Xamarin : Android : System.UnauthorizedAccessException: Access to the path is denied
问题描述
所以我正在尝试创建一个文件,但我得到了System.UnauthorizedAccessException:拒绝访问路径/DownloadJitters".我不确定这是权限问题(我已经尝试写入外部存储以防万一但没有用)还是其他问题.此外,我试图找出一个写这些文件的好地方,因为我希望它们不容易被找到.有任何想法吗?代码如下:
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();
}
}
推荐答案
Ok I 通过将保存位置更改为System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)
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.
这篇关于Xamarin:Android:System.UnauthorizedAccessException:对路径的访问被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!