Read And Write Settings Application

using System;
using System.Linq.Expressions;

namespace HelpfulLinks.App.Settings
{
    public static class ManageSettings
    {
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="expression"></param>
        /// <param name="value"></param>
        public static void ChangeSetting<T>(Expression<Func<T>> expression, dynamic value)
        {
            var memberExpression = expression.Body as MemberExpression;
            if (memberExpression == null) return;
            Properties.Settings.Default[memberExpression.Member.Name] = value;
            Properties.Settings.Default.Save();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static T GetSetting<T>(Expression<Func<T>> expression)
        {
            var memberExpression = expression.Body as MemberExpression;
            return (T)Properties.Settings.Default[memberExpression.Member.Name];
        }
    }
}
Example:
// Read
GetSetting(() => Properties.Settings.Default.UserName);
// Write
ChangeSetting(() => Properties.Settings.Default.UserName, "Hello World !");

Description

You can mangement Properties Settings.

Details

  • Author: Omid Nasri
  • Submitted on: 8/20/2016 8:05:34 PM
  • Language: C#
  • Type: System
  • Views: 912

Double click on the code to select all.

 

;