2

I have a inventory system which is saved in a file and loaded from the same file when its needed. The problem is that the loading system works perfectly on start but when i press the save and exit button, which is supposed to save the inventory and load the main menu scene, it throws a UnauthorizedAccessException: Access to the path exception, even though the file is not set to read-only and the savePath is the same for both loading and saving:

public void Load()
{
    if (File.Exists(string.Concat(Application.persistentDataPath, savePath)))
    {
        IFormatter formatter = new BinaryFormatter();
        Stream stream = new FileStream(string.Concat(Application.persistentDataPath, savePath), FileMode.Open, FileAccess.Read);
        Inventory newContainer = (Inventory)formatter.Deserialize(stream);
        for (int i = 0; i < container.items.Length; i++)
        {
            container.items[i].UpdateSlot(newContainer.items[i].item, newContainer.items[i].amount);
        }
        stream.Close();
    }
}

The start function:

private void Start()
{
    LoadInventory();
}

private void LoadInventory()
{
    inventory.Load();
    equipment.Load();
}

The save funtion:

public void Save()
{
    IFormatter formatter = new BinaryFormatter();
    Stream stream = new FileStream(string.Concat(Application.persistentDataPath, savePath), FileMode.Create, FileAccess.Write);
    formatter.Serialize(stream, container);
    stream.Close();
}

And where the save function is called. When building the game the SceneManager.LoadScene(0); line doesn't run:

public void SaveAndExit()
{
    Time.timeScale = prevTimeScale;
    SaveInventory();

    SceneManager.LoadScene(0);
}

private void SaveInventory()
{
    inventory.Save();
    equipment.Save();
}

The savePath value in the inspector

enter image description here


And the strange thing is that if i build the game the inventory loads correctly from file and if i try to save and exit, the game throws an error however the data is still saved correctly on the file. I feel like i am missing something here that is quite obvious. If you need anymore info please feel free to ask.

Tudoraster
  • 111
  • 12
  • What is `savePath`s value? – starikcetin Aug 15 '21 at 13:10
  • I will edit it into the post – Tudoraster Aug 15 '21 at 13:11
  • Does this answer your question? [Why is access to the path denied?](https://stackoverflow.com/questions/8821410/why-is-access-to-the-path-denied) – starikcetin Aug 15 '21 at 13:14
  • Thanks for the answer, however it doesn't appear to solve the problem. At first I searched the error on google and this popped up. – Tudoraster Aug 15 '21 at 13:14
  • Can you call File.WriteAllText with a dummy "Hello world" text at that path? And can you try a save path without leading path separator char? – yasirkula Aug 15 '21 at 15:28
  • In general you should use `Path.Combine` and remove that `/` .. `Path.Combine` will automatically fill in the correct path separator used by the according file system – derHugo Aug 15 '21 at 15:49
  • @derHugo I tried using Path.Combine however it throws the same error and i removed the / in the inspector. – Tudoraster Aug 15 '21 at 16:57
  • Btw i should metion that the error occurs in the save() function at the Stream stream line – Tudoraster Aug 15 '21 at 17:07
  • @yasirkula it does work to write at the save path with and without the / – Tudoraster Aug 15 '21 at 17:09
  • Oh in general [**STOP using `BinaryFormatter` at all!**](https://learn.microsoft.com/dotnet/standard/serialization/binaryformatter-security-guide) .. and then what is your target platform? – derHugo Aug 15 '21 at 17:24
  • @derHugo my target is windows pc, what should i use insted of the binaryFormatter, sorry i am new to the c# IO System. Currently i am just trying to store my container variable which is a Inventory class instance which has a array of inventorySlots. This is the setup which i followed in the tutorial. – Tudoraster Aug 15 '21 at 17:29
  • If File.WriteAllText did work, then File.WriteAllBytes will probably work, too. I'd recommend you to compare your Stream with File.WriteAllBytes' stream: https://github.com/microsoft/referencesource/blob/5697c29004a34d80acdaf5742d7e699022c64ecd/mscorlib/system/io/file.cs#L978-L982 P.S. I use BinaryFormatter for my save files on Android/iOS and haven't encountered any issues. But if syncing save file with a server is a necessity, then BinaryFormatter may not be preferable for security reasons. – yasirkula Aug 15 '21 at 17:54

0 Answers0