把下面的代码保存为INI.asp即可运行:
1
<%
2
'Power By Tim
3
'文件摘要:INI类
4
'文件版本:3.0
5
'文本创建日期:2:17 2004-12-14
6
'================= 属性说明 ================
7
'INI.OpenFile = 文件路径(使用虚拟路径需在外部定义)
8
'INI.CodeSet = 编码设置,默认为 GB2312
9
'INI.IsTrue = 检测文件是否正常(存在)
10
'================ 方法说明 =================
11
'IsGroup(组名) 检测组是否存在
12
'IsNode(组名,节点名) 检测节点是否存在
13
'GetGroup(组名) 读取组信息
14
'CountGroup() 统计组数量
15
'ReadNode(组名,节点名) 读取节点数据
16
'WriteGroup(组名) 创建组
17
'WriteNode(组,节点,节点数据) 插入/更新节点数据
18
'DeleteGroup(组名) 删除组
19
'DeleteNode(组名,节点名) 删除节点
20
'Save() 保存文件
21
'Close() 清除内部数据(释放)
22
'===============================================
23
24
25
26
Class INI_Class
27
'===============================================
28
Private Stream '// Stream 对象
29
Private FilePath '// 文件路径
30
Public Content '// 文件数据
31
Public IsTrue '// 文件是否存在
32
Public IsAnsi '// 记录是否二进制
33
Public CodeSet '// 数据编码
34
'================================================
35
36
'// 初始化
37
Private Sub Class_Initialize()
38
Set Stream = Server.CreateObject("ADODB.Stream")
39
Stream.Mode = 3
40
Stream.Type = 2
41
CodeSet = "gb2312"
42
IsAnsi = True
43
IsTrue = True
44
End Sub
45
46
47
'// 二进制流转换为字符串
48
Private Function Bytes2bStr(bStr)
49
if Lenb(bStr)=0 Then
50
Bytes2bStr = ""
51
Exit Function
52
End if
53
54
Dim BytesStream,StringReturn
55
Set BytesStream = Server.CreateObject("ADODB.Stream")
56
With BytesStream
57
.Type = 2
58
.Open
59
.WriteText bStr
60
.Position = 0
61
.Charset = CodeSet
62
.Position = 2
63
StringReturn = .ReadText
64
.Close
65
End With
66
Bytes2bStr = StringReturn
67
Set BytesStream = Nothing
68
Set StringReturn = Nothing
69
End Function
70
71
72
'// 设置文件路径
73
Property Let OpenFile(INIFilePath)
74
FilePath = INIFilePath
75
Stream.Open
76
On Error Resume Next
77
Stream.LoadFromFile(FilePath)
78
'// 文件不存在时返回给 IsTrue
79
if Err.Number<>0 Then
80
IsTrue = False
81
Err.Clear
82
End if
83
Content = Stream.ReadText(Stream.Size)
84
if Not IsAnsi Then Content=Bytes2bStr(Content)
85
End Property
86
87
88
'// 检测组是否存在[参数:组名]
89
Public Function IsGroup(GroupName)
90
if Instr(Content,"["&GroupName&"]")>0 Then
91
IsGroup = True
92
Else
93
IsGroup = False
94
End if
95
End Function
96
97
98
'// 读取组信息[参数:组名]
99
Public Function GetGroup(GroupName)
100
Dim TempGroup
101
if Not IsGroup(GroupName) Then Exit Function
102
'// 开始寻找头部截取
103
TempGroup = Mid(Content,Instr(Content,"["&GroupName&"]"),Len(Content))
104
'// 剔除尾部
105
if Instr(TempGroup,VbCrlf&"[")>0 Then TempGroup=Left(TempGroup,Instr(TempGroup,VbCrlf&"[")-1)
106
if Right(TempGroup,1)<>Chr(10) Then TempGroup=TempGroup&VbCrlf
107
GetGroup = TempGroup
108
End Function
109
110
111
'// 检测节点是否存在[参数:组名,节点名]
112
Public Function IsNode(GroupName,NodeName)
113
if Instr(GetGroup(GroupName),NodeName&"=") Then
114
IsNode = True
115
Else
116
IsNode = False
117
End if
118
End Function
119
120
121
'// 创建组[参数:组名]
122
Public Sub WriteGroup(GroupName)
123
if Not IsGroup(GroupName) And GroupName<>"" Then
124
Content = Content & "[" & GroupName & "]" & VbCrlf
125
End if
126
End Sub
127
128
129
'// 读取节点数据[参数:组名,节点名]
130
Public Function ReadNode(GroupName,NodeName)
131
if Not IsNode(GroupName,NodeName) Then Exit Function
132
Dim TempContent
133
'// 取组信息
134
TempContent = GetGroup(GroupName)
135
'// 取当前节点数据
136
TempContent = Right(TempContent,Len(TempContent)-Instr(TempContent,NodeName&"=")+1)
137
TempContent = Replace(Left(TempContent,Instr(TempContent,VbCrlf)-1),NodeName&"=","")
138
ReadNode = ReplaceData(TempContent,0)
139
End Function
140
141
142
'// 写入节点数据[参数:组名,节点名,节点数据]
143
Public Sub WriteNode(GroupName,NodeName,NodeData)
144
'// 组不存在时写入组
145
if Not IsGroup(GroupName) Then WriteGroup(GroupName)
146
147
'// 寻找位置插入数据
148
'/// 获取组
149
Dim TempGroup : TempGroup = GetGroup(GroupName)
150
151
'/// 在组尾部追加
152
Dim NewGroup
153
if IsNode(GroupName,NodeName) Then
154
NewGroup = Replace(TempGroup,NodeName&"="&ReplaceData(ReadNode(GroupName,NodeName),1),NodeName&"="&ReplaceData(NodeData,1))
155
Else
156
NewGroup = TempGroup & NodeName & "=" & ReplaceData(NodeData,1) & VbCrlf
157
End if
158
159
Content = Replace(Content,TempGroup,NewGroup)
160
End Sub
161
162
163
'// 删除组[参数:组名]
164
Public Sub DeleteGroup(GroupName)
165
Content = Replace(Content,GetGroup(GroupName),"")
166
End Sub
167
168
169
'// 删除节点[参数:组名,节点名]
170
Public Sub DeleteNode(GroupName,NodeName)
171
Dim TempGroup
172
Dim NewGroup
173
TempGroup = GetGroup(GroupName)
174
NewGroup = Replace(TempGroup,NodeName&"="&ReadNode(GroupName,NodeName)&VbCrlf,"")
175
if Right(NewGroup,1)<>Chr(10) Then NewGroup = NewGroup&VbCrlf
176
Content = Replace(Content,TempGroup,NewGroup)
177
End Sub
178
179
180
'// 替换字符[实参:替换目标,数据流向方向]
181
' 字符转换[防止关键符号出错]
182
' [ ---> {(@)}
183
' ] ---> {(#)}
184
' = ---> {($)}
185
' 回车 ---> {(1310)}
186
Public Function ReplaceData(Data_Str,IsIn)
187
if IsIn Then
188
ReplaceData = Replace(Replace(Replace(Data_Str,"[","{(@)}"),"]","{(#)}"),"=","{($)}")
189
ReplaceData = Replace(ReplaceData,Chr(13)&Chr(10),"{(1310)}")
190
Else
191
ReplaceData = Replace(Replace(Replace(Data_Str,"{(@)}","["),"{(#)}","]"),"{($)}","=")
192
ReplaceData = Replace(ReplaceData,"{(1310)}",Chr(13)&Chr(10))
193
End if
194
End Function
195
196
197
'// 保存文件数据
198
Public Sub Save()
199
With Stream
200
.Close
201
.Open
202
.WriteText Content
203
.SaveToFile FilePath,2
204
End With
205
End Sub
206
207
208
'// 关闭、释放
209
Public Sub Close()
210
Set Stream = Nothing
211
Set Content = Nothing
212
End Sub
213
214
End Class
215
216
217
Dim INI
218
Set INI = New INI_Class
219
INI.OpenFile = Server.MapPath("Config.ini")
220
'========== 这是写入ini数据 ==========
221
Call INI.WriteNode("SiteConfig","SiteName","Leadbbs极速论坛")
222
Call INI.WriteNode("SiteConfig","Mail","leadbbs@leadbbs.com")
223
INI.Save()
224
'========== 这是读取ini数据 ==========
225
Response.Write("站点名称:"&INI.ReadNode("SiteConfig","SiteName"))
226
%>

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

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226
