I’m getting UnauthorizedAccessException (Access to path is denied) every time I try to read file that my program has written. I tried every advice I’ve found: made sure paths are correct, tried persistentDataPath, tried localDataPath tried custom directories, tried to run as administrator, tried to disable all antivirus and firewall programms.. Still getting this exception.
Here is my code:
private void SerializeObject <T> (T obj, string path, string fileName)
{
BinaryFormatter bf = new BinaryFormatter();
System.IO.Directory.CreateDirectory(path);
FileStream file = File.Open(path + fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
bf.Serialize(file, obj);
file.Close();
}
private T DeserializeObject <T> (string path, string fileName)
{
T obj = default(T);
if (System.IO.Directory.Exists(path))
{
//File.SetAttributes(path, FileAttributes.Normal);
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
print("opened");
obj = (T)bf.Deserialize(file);
print("deserialized");
file.Close();
}
return obj;
}
Please help me find solution. And I repeat that only deserialization throws exception. Nothing else is printed in console. Paths are correct, and data types are correct.