简体   繁体   English

iPhone - 未授权访问异常

[英]iPhone - UnAuthorized Access Exception

This is a question I am sure Xcode developers could also answer. 这是一个我相信Xcode开发人员也可以回答的问题。 I have a screenshot of my code below in Xamarin. 我在Xamarin下面有我的代码截图。

Why am I getting an Unauthorized access exception? 为什么我会收到未经授权的访问异常? I should be able to write to the documents folder right? 我应该能够写到文件夹吗?

在此输入图像描述

Code: 码:

  var webClient = new WebClient();
        //var documentsFolder = Environment.GetFolderPath (Environment.SpecialFolder.Personal); // iOS 7 and earlier
        var documentsFolder = NSBundle.MainBundle.BundlePath;
        var fileNameAndPath = Path.Combine (documentsFolder, "verses.xml");


        if(!File.Exists(documentsFolder)){

            Directory.CreateDirectory(documentsFolder);

            if(!File.Exists(fileNameAndPath)){

                //File.SetAttributes(fileNameAndPath, FileAttributes.Normal);
                File.Create(fileNameAndPath);
                //Throws exception here.

            }

        }

And ERRORs: 和错误:

Access to the path "/var/mobile/Applications/1F95D694-BBA5-4FB3-AE6C-0C2CDD9DEDD8/comexample.app/verses.xml" is denied 访问路径“/var/mobile/Applications/1F95D694-BBA5-4FB3-AE6C-0C2CDD9DEDD8/comexample.app/verses.xml”被拒绝

Access to the path '/private/var/mobile/Applications/1F95D694-BBA5-4FB3-AE6C-0C2CDD9DEDD8/Documents/verses.xml' is denied. 访问路径'/private/var/mobile/Applications/1F95D694-BBA5-4FB3-AE6C-0C2CDD9DEDD8/Documents/verses.xml'被拒绝。

I have tried both paths and I get access denied. 我已尝试过两条路径,但我拒绝访问。

You can't write to your app's bundle path, the folder is readonly. 您无法写入应用程序的包路径,该文件夹是只读的。 I'm also not sure where the Personal folder points to with Xamarin.iOS. 我也不确定Personal文件夹指向Xamarin.iOS的位置。

Use this directory, and you can write to it: 使用此目录,您可以写入:

string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM