Saving to MyDocuments/Saved Games

Hi,

I recently found out that windows will not let applications write directly into the applications directory if it is located in ProgramFiles instead it remaps the saved files to a virtual directory location.

This is a problem as my players find it difficult to locate their saved games files.

What I want to do is save in the correct location under the Userprofile environment variable but I have been unable to work out how to derive the correct path.

I tried this:

profileDirectoryPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments).ToString(), "/Saved Games/LunarFlight/ProfileData/");

But it yields no path…other than the string I am combining on the end.

What is the correct way to go about saving user data files correctly under windows 7?

I worked it out, needed to use this:

string saveFolder = System.Environment.ExpandEnvironmentVariables("%USERPROFILE%\\Saved Games");
1 Like

Have you tried path variables in Application class ? I’m pretty sure they have that covered. Something like persistDataPath, if my memory serves. It might be safer.

If you want this to work cross-platform, you should use the SpecialFolder setup. For this case I would recommend using .ApplicationData or .Personal. Use Path.Combine to join the path elements together. Its implementation is a bit stupid though, so I usually set up a utility method like this:

public static string CombinePath (string first, params string[] others)
{
    foreach (string element in others)
    {
        first = Path.Combine (first, element);
    }

    return first;
}

Let’s you splice multiple elements together in one call, ex:

string path = Utility.CombinePath ("base", "sub", "second", "my.file");