Save system not working. Error occured when trying to save data to file: C:/Users/

Hi,

I’ve recently started to include a save and load system using this youtube channel’s as a guide:

However I have hit a wall for some time now where I don’t know if this is a legitimate error when I close my app in the unity simulator:
System.UnauthorizedAccessException: Access to the path ‘C:\Users\user\AppData\LocalLow*user\app*’ is denied.
at System.IO.FileStream…ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) [0x000e0] in :0
at System.IO.FileStream…ctor (System.String path, System.IO.FileMode mode) [0x00000] in :0

Error debug gives the following
Debug.LogError("Error occured when trying to load data from file: " + fullPath + “\n” + e);
Error occured when trying to save data to file: C:/Users/user/AppData/LocalLow/user/app

Has anyone come across this before? I noticed the error uses forward slashes compared to the debug which uses backwards slashes. Is that something?

Cheers,
Grish

Show your code. Likely something along the lines of:

  • Not properly opening/closing your filestreams (you should use a using statement for this
  • Invalid path construction (nonexistent filepath)

Did you modify that path before posting, or is this what you used in code?

In the latter case the logged in user would have to be named “user” and at the end the “user\app” part may also not be correct. Looks like placeholders you are supposed to replace with actual folder names.

Also take note that if you want your app to be compatible with non-Windows platforms you should use forward slashes in paths, or run each path string through Path.GetFullPath(path) to normalize it.

I had the same problem with this tutorial, but I checked out the package included on the tutorial’s GitHub, and found that he’d made a bunch of updates to the script files after the video was made. For anyone else who runs into this and just followed the tutorial, I would recommend comparing the tutorial project files to the final, and incorporating the updates, and it should work after that.

I have the same problem, and here’s my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.IO;

public class FileDataHandler
{

private string dataDirPath = "";
private string dataFileName = "";

public FileDataHandler(string dataDirPath, string dataFileName) 
{

    this.dataDirPath = dataDirPath;
    this.dataFileName = dataFileName;

}

public GameData Load() 
{

    string fullPath = Path.Combine(dataDirPath, dataFileName);
    GameData loadedData = null;
    if (File.Exists(fullPath)) 
    {
        try
        {

            string dataToLoad = "";
            using (FileStream stream = new FileStream(fullPath, FileMode.Open)) 
            {

                using (StreamReader reader = new StreamReader(stream)) 
                {
                
                    dataToLoad = reader.ReadToEnd();
                
                }
            
            }

            loadedData = JsonUtility.FromJson<GameData>(dataToLoad);

        }
        catch (Exception e) 
        {

            Debug.LogError("Error occured when trying to load data from file: " + fullPath + "\n" + e);
        
        }

    }
    return loadedData;

}

public void Save(GameData data) 
{

    string fullPath = Path.Combine(dataDirPath, dataFileName);
    try
    {

        Directory.CreateDirectory(Path.GetDirectoryName(fullPath));

        string dataToStore = JsonUtility.ToJson(data, true);

        using (FileStream stream = new FileStream(fullPath, FileMode.Create)) 
        {

            using (StreamWriter writer = new StreamWriter(stream)) 
            {
            
                writer.Write(dataToStore);
            
            }
        
        }

    }
    catch (Exception e) 
    {

        Debug.LogError("Error occured when trying to save data to file: " + fullPath + "\n" + e);
    
    }

}

}

In the end, my desire to move on won out over my desire to learn how to do everything myself, and I just bought the EasySave3 plugin from the AssetStore. $60, but worth every penny for the stress it relieved.

You really don’t need to fuss with FileStream and StreamReader/Writer unless you’re doing partial file reads or other low level control over the process.

Otherwise just use File.Read/WriteAllText/Bytes for your bog standard read/write operations. Naturally Applicaton.persistentDataPath should be used to start your file path for save files.

It can be this simple:

// writing
string json = JsonUtility.ToJson(saveData);
string path = Application.persistentDataPath + "/SaveData.txt";
File.WriteAllText(path, json);

// reading
string path = Application.persistentDataPath + "/SaveData.txt";
string json = File.ReadAllText(path);
var saveData = JsonUtility.FromJson<SaveData>(json);