Skip to content

Instantly share code, notes, and snippets.

@trcio
Last active August 29, 2015 14:08
Show Gist options
  • Save trcio/ea4ad8371f726f39cf65 to your computer and use it in GitHub Desktop.
Save trcio/ea4ad8371f726f39cf65 to your computer and use it in GitHub Desktop.
Stores and accesses user preferences between sessions. Based off of Unity's PlayerPrefs class: http://docs.unity3d.com/ScriptReference/PlayerPrefs.html
using System;
using System.Reflection;
using Microsoft.Win32;
/// <summary>
/// Stores and accesses user preferences between sessions.
/// </summary>
public static class UserPrefs
{
private static readonly RegistryKey MainKey;
static UserPrefs()
{
MainKey =
Registry.CurrentUser.CreateSubKey(string.Format("Software\\{0}\\{1}",
GetAttribute<AssemblyCompanyAttribute>().Company, GetAttribute<AssemblyProductAttribute>().Product));
}
private static T GetAttribute<T>() where T : Attribute
{
return (T) Attribute.GetCustomAttribute(Assembly.GetEntryAssembly(), typeof (T), false);
}
/// <summary>
/// Removes all keys and values from the preferences. Use with caution.
/// </summary>
public static void DeleteAll()
{
Array.ForEach(MainKey.GetValueNames(), s => MainKey.DeleteValue(s, false));
}
/// <summary>
/// Removes key and its corresponding value from the preferences.
/// </summary>
public static void DeleteKey(string key)
{
MainKey.DeleteValue(string.Concat("f_", key), false);
MainKey.DeleteValue(string.Concat("i_", key), false);
MainKey.DeleteValue(string.Concat("s_", key), false);
}
/// <summary>
/// Returns the value corresponding to key if it exists. If it doesn't exist, it will return defaultValue.
/// </summary>
public static float GetFloat(string key, float defaultValue)
{
return Convert.ToSingle(MainKey.GetValue(string.Concat("f_", key), defaultValue));
}
/// <summary>
/// Returns the value corresponding to key if it exists. If it doesn't exist, it will return defaultValue.
/// </summary>
public static int GetInt(string key, int defaultValue)
{
return (int) MainKey.GetValue(string.Concat("i_", key), defaultValue);
}
/// <summary>
/// Returns the value corresponding to key if it exists. If it doesn't exist, it will return defaultValue.
/// </summary>
public static string GetString(string key, string defaultValue)
{
return (string) MainKey.GetValue(string.Concat("s_", key), defaultValue);
}
/// <summary>
/// Returns true if key exists in the preferences.
/// </summary>
public static bool HasKey(string key)
{
return (MainKey.GetValue(key, null) != null);
}
/// <summary>
/// Sets the value of the preference identified by key.
/// </summary>
public static void SetFloat(string key, float value)
{
MainKey.SetValue(string.Concat("f_", key), value);
}
/// <summary>
/// Sets the value of the preference identified by key.
/// </summary>
public static void SetInt(string key, int value)
{
MainKey.SetValue(string.Concat("i_", key), value);
}
/// <summary>
/// Sets the value of the preference identified by key.
/// </summary>
public static void SetString(string key, string value)
{
MainKey.SetValue(string.Concat("s_", key), value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment