#region usings
using System.IO;
using System.IO.IsolatedStorage;
using System.Xml.Serialization;
#endregion
namespace YourNamespace
{
#region application settings
public class AppSettings
{
public bool HasRunOnce { get; set; }
public bool IsFullScreen { get; set; }
public bool EnableMusic { get; set; }
public bool EnableSfx { get; set; }
public AppSettings()
{
// Create our default settings
HasRunOnce = true;
EnableMusic = true;
EnableSfx = true;
// Since this is cross platform, you can decide what default values to use for a platform.
// In the case of full screen, phones and XBoxes are always full screen.
// In the phone an XBox applications, we don't let the user change this setting.
#if WINDOWS
IsFullScreen = false;
#else
IsFullScreen = true;
#endif
}
}
#endregion
#region settings manager
static class SettingsManager
{
private static string fileName = "settings.xml";
public static AppSettings Settings = new AppSettings();
public static void LoadSettings()
{
// Create our exposed settings class. This class gets serialized to load/save the settings.
Settings = new AppSettings();
//Obtain a virtual store for application
#if WINDOWS
IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForDomain();
#else
IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();
#endif
// Check if file is there
if (fileStorage.FileExists(fileName))
{
XmlSerializer serializer = new XmlSerializer(Settings.GetType());
StreamReader stream = new StreamReader(new IsolatedStorageFileStream(fileName, FileMode.Open, fileStorage));
try
{
Settings = (AppSettings)serializer.Deserialize(stream);
stream.Close();
}
catch
{
// An error occurred so let's use the default settings.
stream.Close();
Settings = new AppSettings();
// Saving is optional - in this sample we assume it works and the error is due to the file not being there.
SaveSettings();
// Handle other errors here
}
}
else
{
SaveSettings();
}
}
public static void SaveSettings()
{
//Obtain a virtual store for application
#if WINDOWS
IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForDomain();
#else
IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();
#endif
XmlSerializer serializer = new XmlSerializer(Settings.GetType());
StreamWriter stream = new StreamWriter(new IsolatedStorageFileStream(fileName, FileMode.Create, fileStorage));
try
{
serializer.Serialize(stream, Settings);
}
catch
{
// Handle your errors here
}
stream.Close();
}
}
#endregion
}