1
yhj
2024-07-24 5e5d945e91568b973faa27d8ab0bcef99fc4a6c5
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
 
namespace CSFrameworkV5.Core
{
    /// <summary>
    /// 操作INI文件类 
    /// </summary>
    public class IniFile
    {
        private string _path; //INI档案名 
 
        public string IniPath
        {
            get => _path;
            set => _path = value;
        }
 
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        public struct STRINGBUFFER
        {
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)]
            public string szText;
        }
 
        //读写INI文件的API函数 
        [DllImport("kernel32", CharSet = CharSet.Auto)]
        private static extern long WritePrivateProfileString(string section,
            string key, string val, string filePath);
 
        //读写INI文件的API函数 
        [DllImport("kernel32", CharSet = CharSet.Auto)]
        private static extern int GetPrivateProfileString(string section,
            string key, string def, out STRINGBUFFER retVal, int size,
            string filePath);
 
        //类的构造函数,传递INI档案名 
        public IniFile(string INIPath)
        {
            _path = INIPath;
            if (!File.Exists(_path)) CreateIniFile();
        }
 
        /// <summary>
        /// 写INI文件
        /// </summary>
        /// <param name="Section"></param>
        /// <param name="Key"></param>
        /// <param name="Value"></param>
        public void IniWriteValue(string Section, string Key, string Value)
        {
            WritePrivateProfileString(Section, Key, Value, _path);
        }
 
        /// <summary>
        /// 读取INI文件指定关键字的值
        /// </summary>
        /// <param name="Section"></param>
        /// <param name="Key"></param>
        /// <returns></returns>
        public string IniReadValue(string Section, string Key)
        {
            int i;
            STRINGBUFFER RetVal;
            i = GetPrivateProfileString(Section, Key, null, out RetVal, 1024,
                _path);
            var temp = RetVal.szText;
            return temp.Trim();
        }
 
        /// <summary>
        /// 读取INI文件指定关键字的值
        /// </summary>
        /// <param name="Section"></param>
        /// <param name="Key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public string IniReadValue(string Section, string Key,
            string defaultValue)
        {
            int i;
            STRINGBUFFER RetVal;
            i = GetPrivateProfileString(Section, Key, null, out RetVal, 1024,
                _path);
            var temp = RetVal.szText.Trim();
            return string.IsNullOrEmpty(temp) ? defaultValue : temp;
        }
 
        /// <summary>
        /// 创建INI文件
        /// </summary>
        public void CreateIniFile()
        {
            var w = File.CreateText(_path);
            w.Write("");
            w.Flush();
            w.Close();
        }
    }
}