Files, StorageDevice/StorageContainer

Started by
0 comments, last by Nik02 9 years, 5 months ago

I have been trying to find a solution to this for a few hours now and my Google foo is not doing the job.

I have a map editor, with a bunch of tiles. I want to serialize the entire Map class to a file for later loading, I can't for the love of me figure out how to get what every tutorial/blog/stackoverflow answer is talking about, the StorageDevice/StorageContainer.

Am I missing something here?

Even the code samples gave to me by my tutor asks for the StorageContainer:


public static void SaveHighScores(HighScoreData data, string filename)
{
    // Get the path of the save game
    string fullpath = Path.Combine(StorageContainer.TitleLocation, filename);
 
    // Open the file, creating it if necessary
    FileStream stream = File.Open(fullpath, FileMode.OpenOrCreate);
    try
    {
        // Convert the object to XML data and put it in the stream
        XmlSerializer serializer = new XmlSerializer(typeof(HighScoreData));
        serializer.Serialize(stream, data);
    }
    finally
    {
        // Close the file
        stream.Close();
    }
}

Where does File.Open start? Is it the root directory of the machine (User)?

Advertisement

StorageContainer.TitleLocation gets you an absolute path to the directory that represents your game's storage location. Path.Combine combines this absolute path with the file name, which results in an another absolute path.

If File.Open is given an absolute path, it tries to respect it precisely. If a relative path is specified, however, the method will try to find the file relative to the executable of the process, or relative to the working directory of the process. The path relative to the executable cannot usually be assumed to be writable without administrative rights.

Put a breakpoint to the File.Open line to see the actual path.

Niko Suni

This topic is closed to new replies.

Advertisement