SysParam.cs 1.8 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9 10
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Misc;
using System.Net;
using System.IO;
using System.ComponentModel;
using System.Xml.Serialization;
using FLY.Thick.Base.Common;
11
using Newtonsoft.Json;
潘栩锋's avatar
潘栩锋 committed
12 13 14 15

namespace FLY.Thick.Base.Server
{
    //代替TDInitParam
16
    public class SysParam : INotifyPropertyChanged
潘栩锋's avatar
潘栩锋 committed
17 18 19
    {
        #region 属性,成员变量的代理

20 21 22
        /// <summary>
        /// 本机obj服务地址
        /// </summary>
23
        public string Addr { get; set; } = "0.0.0.0:20006";
潘栩锋's avatar
潘栩锋 committed
24 25 26

        #endregion

27
        private string param_path = "sysparam.json";
潘栩锋's avatar
潘栩锋 committed
28 29
        public SysParam()
        {
30

潘栩锋's avatar
潘栩锋 committed
31 32 33 34 35
        }
        public SysParam(string param_path) 
        {
            if (!string.IsNullOrEmpty(param_path))
                this.param_path = param_path;
36 37 38
            if (!Load()) {
                Save();
            }
潘栩锋's avatar
潘栩锋 committed
39
        }
40 41


42
        bool Load()
潘栩锋's avatar
潘栩锋 committed
43
        {
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
            try
            {
                if (File.Exists(param_path))
                {
                    string json = File.ReadAllText(param_path);
                    JsonConvert.PopulateObject(json, this);
                    return true;
                }
            }
            catch
            {
                //异常,没有json 解码失败
            }

            return false;
        }
        public bool Save()
        {
            try
            {
                File.WriteAllText(param_path, JsonConvert.SerializeObject(this, Formatting.Indented));
                return true;
            }
            catch
            {
                //异常,没有json 编码失败

            }
            return false;

潘栩锋's avatar
潘栩锋 committed
74 75 76 77 78 79
        }


        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;
80

潘栩锋's avatar
潘栩锋 committed
81 82
        #endregion

83

潘栩锋's avatar
潘栩锋 committed
84 85
    }
}