2017-07-16 44 views
0

我知道这已经出现了一段时间,但我无法解决我尝试的任何解决方案的错误。UnauthorizedAccessException:访问路径是拒绝Ios

我刚开始测试我的应用程序 - 保存截图到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; 
} 

要WITE文件

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:对路径的访问 “的/ var /瓶/包/应用/ 9DA2D489-2037-451E-87D1-FA7354ECD0D1 /文档” 被拒绝。

回答

1

您保存Application.persistentDataPathApplication.dataPath。请注意,您必须创建一个文件夹并保存到该文件夹​​中,而不是直接保存到此路径。

所以你保存到最终的路径应该是:

Application.persistentDataPath+"Yourfolder/YourFileNale.extension"

Application.persistentDataPath+"Documents/YourFileNale.extension"

总是使用Path.Combine来合并路径,而不是我上面使用的"+"

+0

在我可以创建目录之前,我在CreateImagesDirectory()中得到错误,所以即使文件夹创建没有发生 –

+0

如果出现错误并且不发布它,您希望我做什么? ............'if(!Directory.Exists(Path.GetDirectoryName(yourPath))) { Directory.CreateDirectory(Path.GetDirectoryName(yourPath)); }'......然后.......'File.WriteAllBytes(yourPath,yourData);' – Programmer

+0

好吧,我用代码和错误更新了方法。 CreateImagesDirectory方法中提供了存在选项。你可以请现在检查吗? –