Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'Scripting' started by Grish, Jul 9, 2023.

  1. Grish

    Grish

    Joined:
    Jan 15, 2015
    Posts:
    5
    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 <c2a97e0383e8404c9fc0ae19d58f57f1>:0
    at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode) [0x00000] in <c2a97e0383e8404c9fc0ae19d58f57f1>: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
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,718
    Show your code. Likely something along the lines of:
    1. Not properly opening/closing your filestreams (you should use a
      using
      statement for this
    2. Invalid path construction (nonexistent filepath)
     
    Bunny83 likes this.
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    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.