Access to persistent data path is denied

Hi,

I’m making a save/load system for my multiplayer game and I want to store each player’s position in a separate file where the name of the file is an ID on the player script. When I changed the path to have a variable instead of just writing the file name, I get this error:

UnauthorizedAccessException: Access to the path C:\Users\**********\AppData\LocalLow\DefaultCompany\Server Saving Test\ is denied.

This is my code:

foreach (PlayerMovement player in playersToSave)
{
    string path = Path.Combine(Application.persistentDataPath, player.playerID);
    FileStream stream = new FileStream(path, FileMode.Create);
    BinaryFormatter formatter = new BinaryFormatter();
    if (useBinaryFormatter)
    {
        foreach (PlayerMovement pData in playersToSave)
        {
            PlayerData data = new PlayerData(pData);
            formatter.Serialize(stream, data);
        }
    }
    else
    {
        using (StreamWriter writer = new StreamWriter(stream))
        {
            foreach (PlayerMovement pData in playersToSave)
            {
                PlayerData data = new PlayerData(pData);
                writer.WriteLine(data);
            }
        }
    }
    stream.Close();
}

Thanks :smiley:

1 Like