I know this has come up some time but I am not able to solve the error with any of the solutions I tried.
I just started testing my application - to save a screenshot to ios device Code is -
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;
}
To wite the file
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);
Assuming screenshot is bytes[]
I get the exception that as in subject. I am using Unity.
Error that I get is -
UnauthorizedAccessException: Access to the path "/var/containers/Bundle/Application/9DA2D489-2037-451E-87D1-FA7354ECD0D1/Documents" is denied.
You save with Application.persistentDataPath
not Application.dataPath
. Note that you must create a folder and save inside that folder instead of saving directly to this path.
So the final path you save to should be:
Application.persistentDataPath+"Yourfolder/YourFileNale.extension"
.
or
Application.persistentDataPath+"Documents/YourFileNale.extension"
.
Always use Path.Combine
to Combine paths instead of "+"
I used above.