I've lost file access to Application.persistentDataPath

I’ve had this with both Android and iOS, and both have followed almost the same story. Previously working code started failing with UnauthorizedAccessException. No amount of reverting to previous commits, restarting, uninstalling/reinstalling, or trying on different devices has been able to get rid of it.

The code in question:

string filePath = Path.Combine(Application.persistentDataPath, "PerfReport.csv");
using (StreamWriter writer = new StreamWriter(filePath))

When it happened on Android, I had been writing to a subdirectory under persistentDataPath and was able to work around it by writing directly to persistentDataPath (no idea why that worked).

However, the same thing just happened on iOS, except that I’m already writing directly to persistentDataPath as you can see from the code above.

What can cause this?

Show us the full code with context, including the full path when the exception occurs.

Did you log the path to make sure it is correct?

Is the code perhaps running threaded ie is there a possibility that two writers try to write at the same time?

Well, it looks like I just needed to be forced to answer some questions. I found some old code trying to create the same file that hadn’t been properly deleted. Thanks for your help.