全部源代码如下:
显示源码
1 Class INIClass 2 Private dict,path 3 4 Private Sub Class_Initialize() 5 Set dict = server.CreateObject( " Scripting.Dictionary " ) 6 End Sub 7 Private Sub Class_Terminate() 8 Set dict = Nothing 9 End Sub 10 11 ' 计算.ini文件的实际路径 12 ' 参数: 13 ' fpath ini文件路径,虚拟路径或实际路径 14 ' 返回值: 15 ' ini文件实际路径 16 Private Function address(ByVal fpath) 17 On Error Resume Next 18 address = Server.MapPath(fpath) 19 If Err.Number <> 0 Then address = fpath 20 End Function 21 22 ' 加载ini文件 23 ' 参数: 24 ' fpath ini文件路径,虚拟路径或实际路径 25 Public Function Load(ByVal fpath) 26 path = fpath 27 Dim exComm: Set exComm = New RegExp:exComm.Pattern = " ^\s*;.*\s*$ " 28 Dim exSect: Set exSect = New RegExp:exSect.Pattern = " ^\s*\[\s*([^\[\]]+)\s*\]\s*$ " 29 Dim exPair: Set exPair = New RegExp:exPair.Pattern = " ^\s*([^=]+)\s*=\s*(.*)\s*$ " 30 Dim fso,file,matchs,section 31 Set fso = Server.CreateObject( " Scripting.FileSystemObject " ) 32 Set file = fso.OpenTextFile(address(path), 1 ) 33 Do While Not file.AtEndOfStream 34 StrBuf = file.ReadLine 35 If exComm.Test(StrBuf) Then 36 ElseIf exSect.Test(StrBuf) Then 37 Set matchs = exSect.Execute(StrBuf) 38 section = matchs( 0 ).Submatches( 0 ) 39 If Not dict.Exists(section) Then 40 dict.Add section,server.CreateObject( " Scripting.Dictionary " ) 41 End If 42 ElseIf exPair.Test(StrBuf) Then 43 Set matchs = exPair.Execute(StrBuf) 44 If Not dict.Exists(section) Then 45 dict.Add section,server.CreateObject( " Scripting.Dictionary " ) 46 End If 47 dict.Item(section).Item(matchs( 0 ).Submatches( 0 )) = matchs( 0 ).Submatches( 1 ) 48 End If 49 Loop 50 file.Close 51 Set exComm = Nothing 52 Set exSect = Nothing 53 Set exPair = Nothing 54 Set file = Nothing 55 Set fso = Nothing 56 End Function 57 58 ' 保存ini文件 59 ' 参数: 60 ' fpath ini文件路径,虚拟路径或实际路径 61 Public Function Save(ByVal fpath) 62 If fpath = "" Then fpath = path 63 Dim fso,file,section,key 64 Set fso = Server.CreateObject( " Scripting.FileSystemObject " ) 65 Set file = fso.CreateTextFile(address(fpath), true , true ) 66 For Each section In dict.Keys 67 If trim (section) <> "" Then file.WriteLine " [ " & section & " ] " 68 For Each key In dict.Item(section).Keys 69 file.WriteLine key & " = " & dict.Item(section).Item(key) 70 Next 71 Next 72 End Function 73 74 ' 读取ini文件键值 75 ' 参数: 76 ' section ini文件路径,虚拟路径或实际路径 77 ' key 键名 78 ' def 默认值,若section小节下的key键不存在,则返回此值 79 ' 返回值: 80 ' 读取到的键值 81 Public Function Read(section,key,def) 82 section = trim (section) 83 key = Trim (key) 84 If dict.Exists(section) And dict.Item(section).Exists(key) Then 85 Read = dict.Item(section).Item(key) 86 Else 87 Read = def 88 End if 89 End Function 90 91 ' 设置ini文件键值 92 ' 参数: 93 ' section ini文件路径,虚拟路径或实际路径 94 ' key 键名 95 ' value 键值 96 ' 备注: 97 ' 该方法并不会将键值保存到文件中,要保存到文件请调用Save方法 98 Public Function Write(section,key,value) 99 section = trim (section) 100 key = Trim (key) 101 If Not dict.Exists(section) Then 102 dict.Add section,server.CreateObject( " Scripting.Dictionary " ) 103 End If 104 dict.Item(section).Item(key) = value 105 End Function 106 End Class
调用方法如下:
显示源码
1 Dim ini: Set ini = New INIClass ' 新建INI类 2 ini.Load " db.ini " ' 加载INI文件 3 ini.Read " DataBase " , " uid " , " 211314 " ' 读取DataBase小节下uid键的值,如果这个键不存在,则默认值为211314 4 ini.Write " DataBase " , " pwd " , " hsrzq " ' 设置DataBase小节下pwd键的值为hsrzq 5 ini.Save " a.ini " ' 保存新INI配置文件到a.ini,原始文件db.ini并不改变