forked from wangyx0055/DisklessSystem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IniFile.cs
155 lines (137 loc) · 5.27 KB
/
IniFile.cs
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace NoDiskSystem
{
/// <summary>
/// ini文件类
/// </summary>
public class IniFile
{
private string m_FileName;
public string FileName
{
get { return m_FileName; }
set { m_FileName = value; }
}
[DllImport("kernel32.dll")]
private static extern int GetPrivateProfileInt(
string lpAppName,
string lpKeyName,
int nDefault,
string lpFileName
);
[DllImport("kernel32.dll")]
private static extern int GetPrivateProfileString(
string lpAppName,
string lpKeyName,
string lpDefault,
StringBuilder lpReturnedString,
int nSize,
string lpFileName
);
[DllImport("kernel32.dll")]
private static extern int WritePrivateProfileString(
string lpAppName,
string lpKeyName,
string lpString,
string lpFileName
);
/// <summary>
/// 构造函数
/// </summary>
/// <param name="aFileName">Ini文件路径</param>
public IniFile(string aFileName)
{
this.m_FileName = aFileName;
}
/// <summary>
/// 构造函数
/// </summary>
public IniFile()
{ }
/// <summary>
/// [扩展]读Int数值
/// </summary>
/// <param name="section">节</param>
/// <param name="name">键</param>
/// <param name="def">默认值</param>
/// <returns></returns>
public int ReadInt(string section, string name, int def)
{
return GetPrivateProfileInt(section, name, def, this.m_FileName);
}
/// <summary>
/// [扩展]读取string字符串
/// </summary>
/// <param name="section">节</param>
/// <param name="name">键</param>
/// <param name="def">默认值</param>
/// <returns></returns>
public string ReadString(string section, string name, string def)
{
StringBuilder vRetSb = new StringBuilder(2048);
GetPrivateProfileString(section, name, def, vRetSb, 2048, this.m_FileName);
return vRetSb.ToString();
}
/// <summary>
/// [扩展]写入Int数值,如果不存在 节-键,则会自动创建
/// </summary>
/// <param name="section">节</param>
/// <param name="name">键</param>
/// <param name="Ival">写入值</param>
public void WriteInt(string section, string name, int Ival)
{
WritePrivateProfileString(section, name, Ival.ToString(), this.m_FileName);
}
/// <summary>
/// [扩展]写入String字符串,如果不存在 节-键,则会自动创建
/// </summary>
/// <param name="section">节</param>
/// <param name="name">键</param>
/// <param name="strVal">写入值</param>
public void WriteString(string section, string name, string strVal)
{
WritePrivateProfileString(section, name, strVal, this.m_FileName);
}
/// <summary>
/// 删除指定的 节
/// </summary>
/// <param name="section"></param>
public void DeleteSection(string section)
{
WritePrivateProfileString(section, null, null, this.m_FileName);
}
/// <summary>
/// 删除全部 节
/// </summary>
public void DeleteAllSection()
{
WritePrivateProfileString(null, null, null, this.m_FileName);
}
/// <summary>
/// 读取指定 节-键 的值
/// </summary>
/// <param name="section"></param>
/// <param name="name"></param>
/// <returns></returns>
public string IniReadValue(string section, string name)
{
StringBuilder strSb = new StringBuilder(256);
GetPrivateProfileString(section, name, "", strSb, 256, this.m_FileName);
return strSb.ToString();
}
/// <summary>
/// 写入指定值,如果不存在 节-键,则会自动创建
/// </summary>
/// <param name="section"></param>
/// <param name="name"></param>
/// <param name="value"></param>
public void IniWriteValue(string section, string name, string value)
{
WritePrivateProfileString(section, name, value, this.m_FileName);
}
}
}