- Home /
Write/Read save files on Windows: MyDocuments vs persistentDataPath
I keep reading that all saved data should be located in Application.persistentDataPath, which makes sense for code to be cross-platform compatible. But many games for Windows will save their game saves to MyDocuments/"GameName". I suppose this makes it easier on the user to locate and backup if they wanted.
Is it appropriate to save player data to MyDocuments for Windows and use persistantDataPath for other platforms? Are there any reasons why I should or shouldn't use MyDocuments to store data or is it simply based on the developer's preference?
Answer by Dave-Carlile · May 18, 2016 at 08:15 PM
For windows, "My Documents\My Games\YourGameName\" comes highly recommended. Don't save directly in the My Documents folder because it dirties it. Many games use My Games.
I would suggest using the .NET Environment.GetFolderPath method to get the My Documents folder. I don't believe there's an option to return "My Games" using that unfortunately, but you can get close at least.
Here's what I'm using. Builds the My Games path when compiled for Windows, otherwise uses persistentDataPath. At some point the plan is to use whatever game folder is standard for other platforms, assuming there is some standard method for finding that path.
using Environment = System.Environment;
#if UNITY_STANDALONE_WIN
savedGamesPath =
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments).Replace("\\", "/");
savedGamesPath += "/My Games/MyGameName/";
#else
savedGamesPath = Application.persistentDataPath + "/";
#endif
Answer by FortisVenaliter · May 18, 2016 at 08:10 PM
Part of it is developer preference, yeah, but part of it is also ease of coding. For example, if you code it to always go to My Documents, you need to manually handle problems with file/folder permissions, whereas the persistent data path is safe in that manner. Also, remember that users can have multiple folders link into their libraries (of which Documents is one), so that may further complicate matters. Finally, I believe the persistent data path is user-agnostic... e.g. you can access it from any Windows user, but the documents folder is obviously limited to the user.
Many games not built with Unity will just store to the My Documents folder because they don't have a built-in manager for that, so they have to write their own code, and My Documents is the obvious location.
So, if you're a beginner, it's probably best to just use the built in functions so you don't have to worry about all the extra stuff. If you're up for the challenge, though, anything is possible.
Thanks for the detailed response, I really appreciate it.
Your answer
Follow this Question
Related Questions
Open custom file type 1 Answer
Windows 8 - Windows.Storage WriteTextAsync exception 0 Answers
When writing a txt file, it says sharing violation 3 Answers
Access is denied in Windows 8 App 0 Answers
Standalond built for Windows doesn't include text assets 0 Answers