tangxu
2024-04-30 26d8996e55c33186800031f7c305688035bb3182
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.IO;
using System.Text;
using Yw;
 
namespace IStation.ChEr
{
    internal class SettingsHelper
    {
        /// <summary>
        /// json文件名称
        /// </summary>
        internal static string JsonFileName
        {
            get
            {
                if (string.IsNullOrEmpty(_jsonFileName))
                {
                    _jsonFileName = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "paras_settings.json");
                }
                return _jsonFileName;
            }
        }
 
        private static string _jsonFileName = null;
 
        /// <summary>
        /// 查询
        /// </summary>
        internal static Paras Get()
        {
            if (_appparas == null)
            {
                lock (_locker)
                {
                    if (_appparas == null)
                    {
                        var json = File.ReadAllText(JsonFileName);
                        _appparas = JsonHelper.Json2Object<Paras>(json);
                        if (_appparas == null)
                        {
                            _appparas = new Paras();
                        }
                    }
                }
            }
            return _appparas;
        }
 
        private static Paras _appparas = null;
        private static readonly object _locker = new object();
 
        /// <summary>
        /// 保存
        /// </summary>
        /// <returns></returns>
        internal static bool Save()
        {
            if (_appparas == null)
                return default;
            var json = JsonHelper.Object2FormatJson(_appparas);
            File.WriteAllText(JsonFileName, json, Encoding.UTF8);
            return true;
        }
    }
}