rainmeter-studio/RainmeterStudio/Business/SettingsProvider.cs

64 lines
2.0 KiB
C#
Raw Normal View History

using System;
using System.Configuration;
using System.Linq;
namespace RainmeterStudio.Business
{
public static class SettingsProvider
{
/// <summary>
/// Attempts to retrieve the setting of type T, where T is class
/// </summary>
/// <typeparam name="T">Any class type</typeparam>
/// <param name="name">Name of setting</param>
/// <returns>Retrieved setting, or null if not found</returns>
public static T GetSetting<T> (string name) where T : class
{
var property = Properties.Settings.Default.Properties
.OfType<SettingsProperty>()
.FirstOrDefault(x => String.Equals(x.Name, name));
return (property == null) ? null : (property.DefaultValue as T);
}
/// <summary>
/// Attempts to retrieve the setting of type T
/// </summary>
/// <typeparam name="T">Any type</typeparam>
/// <param name="name">Name of setting</param>
/// <param name="value">Output value</param>
/// <returns>True if attempt was successful</returns>
public static bool TryGetSetting<T>(string name, out T value)
{
var property = Properties.Settings.Default.Properties.OfType<SettingsProperty>().FirstOrDefault(x => x.Name.Equals(name));
if (property != null)
{
value = (T)property.DefaultValue;
return true;
}
else
{
value = default(T);
return false;
}
}
/// <summary>
/// Saves the settings
/// </summary>
public static void SaveSettings()
{
Properties.Settings.Default.Save();
}
/// <summary>
/// Resets settings to default
/// </summary>
public static void ResetSettings()
{
Properties.Settings.Default.Reset();
}
}
}