把下面的代码保存为INI.asp即可运行:
1
<%2
'Power By Tim3
'文件摘要:INI类4
'文件版本:3.05
'文本创建日期:2:17 2004-12-146
'================= 属性说明 ================7
'INI.OpenFile = 文件路径(使用虚拟路径需在外部定义)8
'INI.CodeSet = 编码设置,默认为 GB23129
'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_Class27
'===============================================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 = 340
Stream.Type = 241
CodeSet = "gb2312"42
IsAnsi = True43
IsTrue = True44
End Sub45
46
47
'// 二进制流转换为字符串48
Private Function Bytes2bStr(bStr)49
if Lenb(bStr)=0 Then50
Bytes2bStr = ""51
Exit Function52
End if53
54
Dim BytesStream,StringReturn55
Set BytesStream = Server.CreateObject("ADODB.Stream")56
With BytesStream57
.Type = 258
.Open59
.WriteText bStr60
.Position = 061
.Charset = CodeSet62
.Position = 263
StringReturn = .ReadText64
.Close65
End With66
Bytes2bStr = StringReturn67
Set BytesStream = Nothing68
Set StringReturn = Nothing69
End Function70
71
72
'// 设置文件路径73
Property Let OpenFile(INIFilePath)74
FilePath = INIFilePath75
Stream.Open76
On Error Resume Next77
Stream.LoadFromFile(FilePath)78
'// 文件不存在时返回给 IsTrue79
if Err.Number<>0 Then80
IsTrue = False81
Err.Clear82
End if83
Content = Stream.ReadText(Stream.Size)84
if Not IsAnsi Then Content=Bytes2bStr(Content)85
End Property86
87
88
'// 检测组是否存在[参数:组名]89
Public Function IsGroup(GroupName)90
if Instr(Content,"["&GroupName&"]")>0 Then91
IsGroup = True92
Else93
IsGroup = False94
End if95
End Function96
97
98
'// 读取组信息[参数:组名]99
Public Function GetGroup(GroupName)100
Dim TempGroup101
if Not IsGroup(GroupName) Then Exit Function102
'// 开始寻找头部截取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&VbCrlf107
GetGroup = TempGroup108
End Function109
110
111
'// 检测节点是否存在[参数:组名,节点名]112
Public Function IsNode(GroupName,NodeName)113
if Instr(GetGroup(GroupName),NodeName&"=") Then114
IsNode = True115
Else116
IsNode = False117
End if118
End Function119
120
121
'// 创建组[参数:组名]122
Public Sub WriteGroup(GroupName)123
if Not IsGroup(GroupName) And GroupName<>"" Then124
Content = Content & "[" & GroupName & "]" & VbCrlf125
End if126
End Sub127
128
129
'// 读取节点数据[参数:组名,节点名]130
Public Function ReadNode(GroupName,NodeName)131
if Not IsNode(GroupName,NodeName) Then Exit Function132
Dim TempContent133
'// 取组信息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 Function140
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 NewGroup153
if IsNode(GroupName,NodeName) Then154
NewGroup = Replace(TempGroup,NodeName&"="&ReplaceData(ReadNode(GroupName,NodeName),1),NodeName&"="&ReplaceData(NodeData,1))155
Else156
NewGroup = TempGroup & NodeName & "=" & ReplaceData(NodeData,1) & VbCrlf157
End if158
159
Content = Replace(Content,TempGroup,NewGroup)160
End Sub161
162
163
'// 删除组[参数:组名]164
Public Sub DeleteGroup(GroupName)165
Content = Replace(Content,GetGroup(GroupName),"")166
End Sub167
168
169
'// 删除节点[参数:组名,节点名]170
Public Sub DeleteNode(GroupName,NodeName)171
Dim TempGroup172
Dim NewGroup173
TempGroup = GetGroup(GroupName)174
NewGroup = Replace(TempGroup,NodeName&"="&ReadNode(GroupName,NodeName)&VbCrlf,"")175
if Right(NewGroup,1)<>Chr(10) Then NewGroup = NewGroup&VbCrlf176
Content = Replace(Content,TempGroup,NewGroup)177
End Sub178
179
180
'// 替换字符[实参:替换目标,数据流向方向]181
' 字符转换[防止关键符号出错]182
' [ ---> {(@)}183
' ] ---> {(#)}184
' = ---> {($)}185
' 回车 ---> {(1310)}186
Public Function ReplaceData(Data_Str,IsIn)187
if IsIn Then188
ReplaceData = Replace(Replace(Replace(Data_Str,"[","{(@)}"),"]","{(#)}"),"=","{($)}")189
ReplaceData = Replace(ReplaceData,Chr(13)&Chr(10),"{(1310)}")190
Else191
ReplaceData = Replace(Replace(Replace(Data_Str,"{(@)}","["),"{(#)}","]"),"{($)}","=")192
ReplaceData = Replace(ReplaceData,"{(1310)}",Chr(13)&Chr(10))193
End if194
End Function195
196
197
'// 保存文件数据198
Public Sub Save()199
With Stream200
.Close201
.Open202
.WriteText Content203
.SaveToFile FilePath,2204
End With205
End Sub206
207
208
'// 关闭、释放209
Public Sub Close()210
Set Stream = Nothing211
Set Content = Nothing212
End Sub213
214
End Class215
216
217
Dim INI218
Set INI = New INI_Class219
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
%>