UnauthorizedAccessException:访问path是拒绝Ios

我知道这已经出现了一段时间,但我无法解决我尝试的任何解决scheme的错误。

我刚开始testing我的应用程序 – 将截图保存到ios设备代码是 –

string GetiPhoneDocumentsPath() { string path = Application.dataPath.Substring(0, Application.dataPath.Length - 5); path = path.Substring(0, path.LastIndexOf("/")); return path + "/Documents/"; } string CreateImagesDirectory(string documentsPath) { //System.IO.File.SetAttributes (documentsPath, FileAttributes.Normal); string imagePath = documentsPath +"magicimages/"; if (Directory.Exists(imagePath)) {return imagePath;} DirectoryInfo t = new DirectoryInfo(documentsPath); //Directory.CreateDirectory(imagePath); t.CreateSubdirectory("magicimages"); System.IO.File.SetAttributes (imagePath, FileAttributes.Normal); return imagePath; } 

要文件

  Debug.Log("Do nothing actually as we need to save to persistent data path"); string documentsPathIphone = GetiPhoneDocumentsPath(); Debug.Log ("document path" + documentsPathIphone); string imagePath = CreateImagesDirectory (documentsPathIphone); //Path = imagePath + fileName; Debug.Log ("path iphone" + Path); Debug.Log("Appllicarion data path -->" + Application.dataPath); //string savepath = Application.dataPath.Replace ("game.app/Data", "/Documents/"); System.IO.File.WriteAllBytes (System.IO.Path.Combine(Path , fileName), screenshot); 

假设屏幕截图是字节[]

我得到的例外,如在主题。 我正在使用Unity。

我得到的错误是 –

UnauthorizedAccessException:拒绝访问path“/ var / containers / Bundle / Application / 9DA2D489-2037-451E-87D1-FA7354ECD0D1 / Documents”。

您保存与Application.persistentDataPath Application.dataPath 。 请注意,您必须创build一个文件夹并保存在该文件夹内,而不是直接保存到此path。

所以你保存的最终path应该是:

Application.persistentDataPath+"Yourfolder/YourFileNale.extension"

要么

Application.persistentDataPath+"Documents/YourFileNale.extension"

总是使用Path.Combine合并path,而不是上面使用的"+"

Interesting Posts