ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

Read And Write Settings Application

You can mangement Properties Settings.

Source

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 !");

Author: Omid Nasri

Submitted on: 20 aug. 2016

Language: C#

Type: System

Views: 3197