-
Notifications
You must be signed in to change notification settings - Fork 4
/
IniVarLib.pas
93 lines (81 loc) · 2.75 KB
/
IniVarLib.pas
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
unit IniVarLib;
interface
uses IniFiles, variants;
//简单数据类型
type
TIniVarType = (stInt, stFloat, stString, stDateTime, stDate, stTime, stBoolean);
/// <summary>
/// 读Ini文件的函数
/// </summary>
/// <param name="FileName">Ini文件名</param>
/// <param name="Section">节点</param>
/// <param name="Name">字段名</param>
/// <param name="xDataType">简单数据类型</param>
/// <param name="DefaultValue">默认值</param>
/// <returns>返回变体类型</returns>
function ReadIniValue(const FileName, Section, Name: string;
xDataType: TIniVarType; DefaultValue: variant): variant;
procedure WriteIniValue(const FileName, Section, Name: string;
Value: variant; xDataType: TIniVarType);
implementation
function ReadIniValue(const FileName, Section, Name: string;
xDataType: TIniVarType; DefaultValue: variant): variant;
begin
try
with TIniFile.Create(FileName) do
try
if xDataType = stString then
Result := ReadString(Section, Name, DefaultValue)
else if xDataType = stInt then
Result := ReadInteger(Section, Name, DefaultValue)
else if xDataType = stFloat then
Result := ReadFloat(Section, Name, DefaultValue)
else if xDataType = stDateTime then
Result := ReadDateTime(Section, Name, DefaultValue)
else if xDataType = stDate then
Result := ReadDate(Section, Name, DefaultValue)
else if xDataType = stTime then
Result := ReadTime(Section, Name, DefaultValue)
else if xDataType = stBoolean then
Result := ReadBool(Section, Name, DefaultValue);
finally
Free;
end;
except
end;
end;
/// <summary>
/// 写INI文件的函数
/// </summary>
/// <param name="FileName">ini文件名</param>
/// <param name="Section">节点名</param>
/// <param name="Name">字段名</param>
/// <param name="Value">字段值</param>
/// <param name="xDataType">简单类型</param>
procedure WriteIniValue(const FileName, Section, Name: string;
Value: variant; xDataType: TIniVarType);
begin
try
with TIniFile.Create(FileName) do
try
if xDataType = stString then
WriteString(Section, Name, VarToStr(Value))
else if xDataType = stInt then
WriteInteger(Section, Name, Value)
else if xDataType = stFloat then
WriteFloat(Section, Name, Value)
else if xDataType = stDateTime then
WriteDateTime(Section, Name, VarToDateTime(Value))
else if xDataType = stDate then
WriteDate(Section, Name, VarToDateTime(Value))
else if xDataType = stTime then
WriteTime(Section, Name, VarToDateTime(Value))
else if xDataType = stBoolean then
WriteBool(Section, Name, Value);
finally
Free;
end;
except
end;
end;
end.