Permission denied when creating FileStream

I am currently developing an application for VisionPro that needs to store a temporary file on the device.

using System.IO;

var filename = new String( "myfile.txt");
var dirPath = new String(UnityEngine.Application.dataPath + "/");
FileStream outfile = new FileStream( dirPath + filename, FileMode.OpenOrCreate);

This leads to the following exception in the FileStream constructor:

System.UnauthorizedAccessException: Access to the path "/private/var/containers/Bundle/Application/########-####-####-####-############/myapp.app/Data/myfile.txt" is denied.

Do I need to request additional permissions? How do I do this on VisionOS?

VisionOS generally behaves the same as iOS. If you check the documentation of Application.dataPath, you’ll note that it says:

this folder is read only, use Application.persistentDataPath to save data

If it’s really a temporary file, though, you might want to use Application.temporaryCachePath instead.

Thank you! I shouldn’t have stopped reading the documentation after seeing that there is no specific entry for VisionOS.

1 Like