jt
2021-06-10 5d0d028456874576560552f5a5c4e8b801786f11
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
67
68
69
70
71
72
73
74
75
76
77
78
79
/********************************************************************************
 
** auth: DBS
 
** date: 2018/12/11 14:56:55
 
** desc: 尚未编写描述
 
** Ver.:  V1.0.0
 
*********************************************************************************/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace HH.WMS.Common
{
    public class InIHelper
    {
        private static string FileName = Application.StartupPath + "\\AppConfig.ini";
        /// <summary>
        /// 读取配置文件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static T ReadConfig<T>(string section, string key)
        {
            try
            {
                if (File.Exists(FileName))
                {
                    IniFile f = new IniFile(FileName);
                    string value = f.ReadContentValue(section, key);
 
                    if (String.IsNullOrWhiteSpace(value))
                        return default(T);
 
                    if (typeof(T).IsEnum)
                        return (T)Enum.Parse(typeof(T), value, true);
 
                    return (T)Convert.ChangeType(value, typeof(T));
                }
                else
                {
                    return default(T);
                }
            }
            catch (Exception)
            {
                return default(T);
            }
 
        }
 
        /// <summary>
        /// 写配置文件
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void WriteConfig(string section, string key, string value)
        {
            //如果文件不存在,则创建
            if (!File.Exists(FileName))
            {
                using (FileStream myFs = new FileStream(FileName, FileMode.Create)) { }
            }
 
            IniFile f = new IniFile(FileName);
            f.WriteContentValue(section, key, value);
        }
    }
}