Game Development Reference
In-Depth Information
IsolatedStorageFile storage = null;
if (ApplicationDeployment.IsNetworkDeployed)
{
storage = IsolatedStorageFile.GetUserStoreForApplication();
}
else
{
storage = IsolatedStorageFile.GetUserStoreForDomain();
}
return storage;
}
Now that we have retrieved the isolated storage reference, we can begin saving files
to it. Any type of data can be saved into isolated storage, but the purpose of this
chapter is to show application state persistence.
Our system is going to operate on a single file that will store all the persisted infor-
mation, so we are going to want a data structure to hold our state information
while in memory. This will be accomplished through the use of the hashtable col-
lection. A hashtable will be used to store the state information referenced by
unique key. The save will serialize the hashtable into the isolated storage, while the
load will deserialize the hashtable from the isolated storage and back into object
form. Hashtable cannot be serialized to XML because it inherits from an interface,
but BinaryFormatter can be used instead to serialize it to binary. The following code
shows the save routine for the isolated storage interface.
public static void WriteSetting<KEY, TYPE>(string fileName,
KEY key,
TYPE setting)
{
try
{
IsolatedStorageFile storage = OpenIsolatedStorage();
BinaryFormatter formatter = new BinaryFormatter();
Hashtable settings = null;
using (Stream loadStream = new IsolatedStorageFileStream(fileName,
FileMode.OpenOrCreate, storage))
{
try
Search Nedrilad ::




Custom Search