Click here to Skip to main content
15,898,134 members
Articles / Programming Languages / C#

Project Tool

Rate me:
Please Sign up or sign in to vote.
4.69/5 (10 votes)
23 Sep 2007CPOL3 min read 54.6K   1.7K   73  
Backup your C# solution and projects for archive or source code sharing. Temporary or unused files are excluded.
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace QiHe.CodeLib
{
    public class AppSettings
    {
        protected class ControlBinding
        {
            public Control Control;
            public string PropertyName;
            public string DataMemberName;

            public ControlBinding(Control control, string propertyname, string datamembername)
            {
                Control = control;
                PropertyName = propertyname;
                DataMemberName = datamembername;
            }
        }

        protected List<ControlBinding> ControlBindings = new List<ControlBinding>();
        public void Set(Control control, string PropertyName, string DataMemberName)
        {
            Binding binding = new Binding(PropertyName, this, DataMemberName);
            binding.FormattingEnabled = true;
            binding.DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
            control.DataBindings.Add(binding);
            ControlBindings.Add(new ControlBinding(control, PropertyName, DataMemberName));
        }

        public void Save()
        {
            WinApp.SaveConfig(this);
        }

        public void Save(string settingsFile)
        {
            XmlFile.Save(settingsFile, this);
        }

        public void UpdateControls()
        {
            foreach (ControlBinding binding in ControlBindings)
            {
                object propertyValue = GetPropertyValue(this, binding.DataMemberName);
                SetPropertyValue(binding.Control, binding.PropertyName, propertyValue);
            }
        }

        public void UpdateControl(Control control)
        {
            foreach (ControlBinding binding in ControlBindings)
            {
                if (binding.Control == control)
                {
                    object propertyValue = GetPropertyValue(this, binding.DataMemberName);
                    SetPropertyValue(binding.Control, binding.PropertyName, propertyValue);
                }
            }
        }

        public static string InferSettingsFile(string partnerFile)
        {
            string directory = FileHelper.GetDirectory(partnerFile);
            string fileName = Path.GetFileName(WinApp.ConfigFile);
            return Path.Combine(directory, fileName);
        }

        public static object GetPropertyValue(object obj, string propertyName)
        {
            System.Reflection.PropertyInfo propertyInfo
                 = obj.GetType().GetProperty(propertyName);
            if (propertyInfo != null)
            {
                return propertyInfo.GetValue(obj, null);
            }
            return null;
        }

        public static void SetPropertyValue(object obj, string propertyName, object propertyValue)
        {
            System.Reflection.PropertyInfo propertyInfo
                 = obj.GetType().GetProperty(propertyName);
            if (propertyInfo != null)
            {
                propertyInfo.SetValue(obj, propertyValue, null);
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect YunCheDa Hangzhou
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions