现在的位置: 首页 > 综合 > 正文

自定义config文件

2013年03月14日 ⁄ 综合 ⁄ 共 9175字 ⁄ 字号 评论关闭

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Xml.Serialization;

using System.Text;

 

using System.IO;

 

using System.Xml;

 

namespace FutureDBDecompositionTools.Config

{

    [Serializable]

   public  class Setting

    {

        static readonly string xmlFileName = AppDomain.CurrentDomain.BaseDirectory + "DecompositionSetting.xml";

       static private Setting setting ;

 

        public static Setting SettingInstance

        {

            get {

                if (setting == null)

                {

 

                    GetSettingFromXmlFile();

 

                    if (setting == null)

                    {

                        setting = new Setting();

                    }

                }

                return setting; 

            }

            set {

 

                setting = value; 

            }

        }

 

        private static   void GetSettingFromXmlFile()

        {

 

            FileInfo file = new FileInfo(xmlFileName);

            if (File.Exists(file.ToString()))

            {

                string xml = File.ReadAllText(file.FullName);

 

                if ((xml!=null)  && (xml != string.Empty))

                {

                    setting = Logic.SimpleSerializer.Deserialize(typeof(Setting), xml) as 

Setting;

                }

 

            }

 

 

        }

 

        public    void SaveSettingToXmlFile()

        {

            string xml = Logic.SimpleSerializer.Serialize<Setting>(setting);

 

            FileInfo file = new FileInfo(xmlFileName);

            if (File.Exists(file.ToString()))

            {

                file.Delete();                

            }

            File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + "DecompositionSetting.xml", xml);

        }

 

 

        private Setting() { }

 

 

        private string matters_Offline;

 

        [XmlElement("Matters_Offline")]

        public string Matters_Offline

        {

            get

            {

                return matters_Offline;

            }

            set

            {

                matters_Offline = value;

            }

        }

 

 

        string orderDatas_Offline;

 

        [XmlElement("OrderDatas_Offline")]

        public string OrderDatas_Offline

        {

            get

            {

                return orderDatas_Offline;

            }

            set

            {

                orderDatas_Offline = value;

            }

        }

 

        string distributes_Offline;

 

        [XmlElement("Distributes_Offline")]

        public string Distributes_Offline

        {

            get

            {

                return distributes_Offline;

            }

            set

            {

                distributes_Offline = value;

            }

        }

 

        string jobs_Offline;

 

        [XmlElement("Jobs_Offline")]

        public string Jobs_Offline

        {

            get

            {

                return jobs_Offline;

            }

            set

            {

                jobs_Offline = value;

            }

        }

 

 

        string parentDatas_Offline;

 

        [XmlElement("ParentDatas_Offline")]

        public string ParentDatas_Offline

        {

            get

            {

                return parentDatas_Offline;

            }

            set

            {

                parentDatas_Offline = value;

            }

        }

 

 

        string childDatas_Offline;

 

        [XmlElement("ChildDatas_Offline")]

        public string ChildDatas_Offline

        {

            get

            {

                return childDatas_Offline;

            }

            set

            {

                childDatas_Offline = value;

            }

        }

 

        string futureManagement_Offline;

 

        [XmlElement("FutureManagement_Offline")]

        public string FutureManagement_Offline

        {

            get

            {

                return futureManagement_Offline;

            }

            set

            {

                futureManagement_Offline = value;

            }

        }

 

        string decompositionOrders_Offline;

 

        [XmlElement("DecompositionOrders_Offline")]

        public string DecompositionOrders_Offline

        {

            get

            {

                return decompositionOrders_Offline;

            }

            set

            {

                decompositionOrders_Offline  = value;

            }

        }

 

        string offline;

 

        [XmlElement("Offline")]

        [System.Configuration.DefaultSettingValueAttribute("False")]

        public string Offline

        {

            get

            {

                return offline;

            }

            set

            {

                offline = value;

            }

        }

 

 

        string dBAddress_Online;

        [XmlElement("DBAddress_Online")]

        public string DBAddress_Online

        {

            get

            {

                return dBAddress_Online;

            }

            set

            {

                dBAddress_Online = value;

            }

        }

 

 

        string ddAddress_Offline;

 

        [XmlElement("DBAddress_Offline")]

        [System.Configuration.DefaultSettingValueAttribute("")]

        public string DBAddress_Offline

        {

            get

            {

                return ddAddress_Offline;

            }

            set

            {

                ddAddress_Offline = value;

            }

        }

 

        string accounts_Offline;

 

        [XmlElement("Accounts_Offline")]

        public string Accounts_Offline

        {

            get

            {

                return accounts_Offline;

            }

            set

            {

                accounts_Offline = value;

            }

        }

 

        string dBAddress_Decomposition;

 

        [XmlElement("DBAddress_Decomposition")]

        public string DBAddress_Decomposition

        {

            get

            {

                return dBAddress_Decomposition;

            }

            set

            {

                dBAddress_Decomposition = value;

            }

        }

 

        string decompositionOrders_Online;

 

        [XmlElement("DecompositionOrders_Online")]

        public string DecompositionOrders_Online

        {

            get

            {

               return decompositionOrders_Online;

            }

            set

            {

                decompositionOrders_Online = value;

            }

        }

 

        public string this[string propertyName]

        {

            get

            {

                return (GetProperty(propertyName));

            }

            set

            {

                SetProperty(propertyName,value);

            }

        }

 

        void SetProperty(string propertyName,string Value)

        {

            switch (propertyName)

            {

                case "Matters_Offline":

                    setting.Matters_Offline = Value;

                    break;

                case "OrderDatas_Offline":

                    setting.OrderDatas_Offline = Value;

                    break;

                case "Distributes_Offline":

                    setting.Distributes_Offline = Value;

                    break;

                case "Jobs_Offline":

                    setting.Jobs_Offline = Value;

                    break;

                case "ParentDatas_Offline":

                    setting.ParentDatas_Offline = Value;

                    break;

                case "ChildDatas_Offline":

                    setting.ChildDatas_Offline = Value;

                    break;

                case "FutureManagement_Offline":

                    setting.FutureManagement_Offline = Value;

                    break;

                case "DecompositionOrders_Offline":

                    setting.DecompositionOrders_Offline = Value;

                    break;

                case "Offline":

                    setting.Offline = Value;

                    break;

                case "DBAddress_Online":

                    setting.DBAddress_Online = Value;

                    break;

                case "DBAddress_Offline":

                    setting.DBAddress_Offline = Value;

                    break;

                case "Accounts_Offline":

                    setting.Accounts_Offline = Value;

                    break;

                case "DBAddress_Decomposition":

                    setting.DBAddress_Decomposition = Value;

                    break;

                case "DecompositionOrders_Online":

                    setting.DecompositionOrders_Online = Value;

                    break;

                default:

                    break;

 

            }

        }

 

        string GetProperty(string propertyName)

        {

            string Value = string.Empty;

 

            switch (propertyName)

            {

                case "Matters_Offline":

                    Value = setting.Matters_Offline;

                    break;

                case "OrderDatas_Offline":

                    Value = setting.OrderDatas_Offline;

                    break;

                case "Distributes_Offline":

                    Value = setting.Distributes_Offline;

                    break;

                case "Jobs_Offline":

                    Value = setting.Jobs_Offline;

                    break;

                case "ParentDatas_Offline":

                    Value = setting.ParentDatas_Offline;

                    break;

                case "ChildDatas_Offline":

                    Value = setting.ChildDatas_Offline;

                    break;

                case "FutureManagement_Offline":

                    Value = setting.FutureManagement_Offline;

                    break;

                case "DecompositionOrders_Offline":

                    Value = setting.DecompositionOrders_Offline;

                    break;

                case "Offline":

                    Value = setting.Offline;

                    break;

                case "DBAddress_Online":

                    Value = setting.DBAddress_Online;

                    break;

                case "DBAddress_Offline":

                    Value = setting.DBAddress_Offline;

                    break;

                case "Accounts_Offline":

                    Value = setting.Accounts_Offline;

                    break;

                case "DBAddress_Decomposition":

                    Value = setting.DBAddress_Decomposition;

                    break;

                case "DecompositionOrders_Online":

                    Value = setting.DecompositionOrders_Online;

                    break;

                default:

                    break;

            }

 

            return Value;

 

        }

 

    }

}

public class SimpleSerializer
    {
        /// <summary>
        /// 序列化对象
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="t">对象</param>
        /// <returns></returns>
        public static string Serialize<T>(T t)
        {
            using (StringWriter sw = new StringWriter())
            {
                XmlSerializer xz = new XmlSerializer(t.GetType());
                xz.Serialize(sw, t);
                return sw.ToString();
            }
        }
        /// <summary>
        /// 反序列化为对象
        /// </summary>
        /// <param name="type">对象类型</param>
        /// <param name="s">对象序列化后的Xml字符串</param>
        /// <returns></returns>
        public static object Deserialize(Type type, string s)
        {
            using (StringReader sr = new StringReader(s))
            {
                XmlSerializer xz = new XmlSerializer(type);
                return xz.Deserialize(sr);
            }
        }
    }
//////////////////////////////
    Config.Setting.SettingInstance[property] = item.SubItems[1].Text;
  //保存到xml文件
            Config.Setting.SettingInstance.SaveSettingToXmlFile();
////////////////////////////
  item.SubItems[1].Text = Config.Setting.SettingInstance[property].ToString();

抱歉!评论已关闭.