ASP读取INI的方法
利用ASP读取INI的文件 INI的文件如下:
[哈哈123]
Server=111111
[2哈3哈4]
Server=22222
DB=33333
具体ASP要求:
1.读取所有INI的节名([]里的)下Server 和 DB名 全部都列表出来 每50个一页 可以翻页
2.输入框写入一个 然后搜索披对节名是否有这个 有的话就显示出该节名Server 和 DB名
3.以上两个例子都同有一个功能下拉框选择INI文本
4.第一个例子需要按数字大小排列,比如第一个例子是按Server 或者 DB排列 从数字大排列到小
例如:
第一个的要求例子:
用户名 地址 要求
哈哈123 111111
2哈3哈4 22222 33333
...
...
...
...
第一页 上一页 下一页 最后一页
第二个要求的例子:
请输入用户名: 确定
用户名: *****
地址: *****
要求: ******
第三个要求的例子:
请选择INI文本 (123.ini,456.ini,789.ini)
选择后查询该ini里的内容
[解决办法]
smsvote.ini --------------------------------- [smsvote] server=(local) db=smsvote user=sa password=123 [db2vote] server=192.168.0.1 db=db2 user=sa password= <%set inifiledictionary = createobject("scripting.dictionary") sub inifileload(byval filspc) inifiledictionary.removeall filspc = lcase(filspc) if left(filspc, 1) = "p" then physical path phypth = mid(filspc, instr(filspc, "=") + 1) else virtual path phypth = server.mappath(mid(filspc, instr(filspc, "=") + 1)) end if set filsys = createobject("scripting.filesystemobject") set inifil = filsys.opentextfile(phypth, 1) do while not inifil.atendofstream strbuf = inifil.readline if strbuf <> "" then there is data on this line if left(strbuf, 1) <> ";" then its not a comment if left(strbuf, 1) = "[" then its a section header hdrbuf = mid(strbuf, 2, len(strbuf) - 2) else its a value strptr = instr(strbuf, "=") altbuf = lcase(hdrbuf & " ¦" & left(strbuf, strptr - 1)) do while inifiledictionary.exists(altbuf) altbuf = altbuf & "_" loop inifiledictionary.add altbuf, mid(strbuf, strptr + 1) end if end if end if loop inifil.close set inifil = nothing set filsys = nothing end sub function inifilevalue(byval valspc) dim ifarray strptr = instr(valspc, " ¦") valspc = lcase(valspc) if strptr = 0 then they want the whole section strbuf = "" strptr = len(valspc) + 1 valspc = valspc + " ¦" ifarray = inifiledictionary.keys for i = 0 to inifiledictionary.count - 1 if left(ifarray(i), strptr) = valspc then this is from the section if strbuf <> "" then strbuf = strbuf & "~" end if strbuf = strbuf & ifarray(i) & "=" & inifiledictionary(ifarray(i)) end if next else they want a specific value strbuf = inifiledictionary(valspc) end if inifilevalue = strbuf end function function chr(section,key) char1=inifilevalue(section) searchstring =char1 searchchar = key mypos=instr(1,searchstring,searchchar,1) char2=section+key char1=mid(char1,mypos+len(key)+1,len(char1)-mypos+1) searchstring =char1 searchchar = "~" mypos=instr(1,searchstring,searchchar,1) if mypos<>0 then char1=mid(char1,1,mypos-1) else char1=mid(char1,1) end if chr = char1 end function on error resume next dim conn,connstr,dbuid,dbpwd,dbname,dbip call inifileload("virtual=smsvote.ini") 配置文件的名字 dbuid=chr("smsvote","user") section="smsvote",key="user" dbpwd=chr("smsvote","password") section="smsvote",key="password" dbname=chr("smsvote","db") section="smsvote",key="db" dbip=chr("smsvote","server") section="smsvote",key="server" %>
[解决办法]
Dim regSet reg = New RegExpPublic Function RegMatch(ByVal pattern, ByVal flag, ByVal data, ByRef ret) Dim matches, match, i reg.Pattern = pattern reg.Global = False reg.IgnoreCase = CBool(InStr(flag, "i") > 0) Set matches = reg.Execute(data) RegMatch = CBool(matches.Count > 0) If RegMatch Then Set match = matches(0).SubMatches ReDim ret(match.Count) ret(0) = matches(0).Value For i = 1 To match.Count ret(i) = match(i - 1) Next Set match = Nothing End If Set matches = NothingEnd FunctionClass INI_ImplPrivate fso, hashPrivate Sub Class_Initialize() Set hash = Server.CreateObject("Scripting.Dictionary") Set fso = Server.CreateObject("Scripting.FileSystemObject")End SubPrivate Sub Class_Terminate() Set fso = Nothing Set hash = NothingEnd SubPublic Property Get Sections() Sections = hash.KeysEnd PropertyPublic Property Get Section(ByVal key) If Not hash.Exists(key) Then Err.Raise vbObjectError + 1, "INI.Section", "Missing section: " & key End If Set Section = hash(key)End PropertyPublic Default Property Get Item(ByVal key, ByVal name) If Not hash.Exists(key) Then Err.Raise vbObjectError + 1, "INI.Item", "Missing section: " & key End If Item = Section(key)(name)End PropertyPublic Sub Load(ByVal path) If Not fso.FileExists(path) Then Err.Raise vbObjectError + 1, "INI.Load", "Missing ini file: " & path End If Dim file, line, arr Dim section, sectionName Set file = fso.OpenTextFile(path) Do While Not file.AtEndOfStream line = file.ReadLine() If RegMatch("^\[(.+?)\]$", "", line, arr) Then sectionName = arr(1) Set hash(sectionName) = Server.CreateObject("Scripting.Dictionary") ElseIf RegMatch("^([^;=]+)=([^;]+)", "", line, arr) And sectionName <> "" Then hash(sectionName)(arr(1)) = arr(2) End If Loop Set file = NothingEnd SubEnd ClassDim ini, key, nameSet ini = New INI_Implini.load Server.MapPath("/root/path.ini")For Each key In ini.Sections For Each name In ini.Section(key).Keys Response.Write "ini(" & key & ", " & name & ") = " & ini(key, name) & "<br/>" NextNext