日期:2010-04-17 10:15:00 来源:本站整理
用ASP编程读INI配置文件的函数[网站编程]
本文“用ASP编程读INI配置文件的函数[网站编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
要求:
可以读取按照 INI文档的SeCtion和Key来读出呼应的Value.
比方一个配置文档
SMSVote.ini
---------------------------------
- [SMSVote]
- Server=(loCal)
- DB=SMSVote
- User=sa
- PassWord=123
- [DB2DatcbaseVote]
- Server=192.168.0.1
- DB=DB2Datcbase
- User=sa
- PassWord=
---------------------------------
主体程式(method) :
inifile.asp
-----------------------------------------------
- <%
- 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
- 'It's not a Comment
- if left(StrBuf, 1) = "[" then
- 'It's a seCtion header
- HdrBuf = mid(StrBuf, 2, len(StrBuf) - 2)
- else
- 'It's a value
- StrPtr = instr(StrBuf, "=")
- AltBuf = lCase(HdrBuf & " ¦" & left(StrBuf, StrPtr - 1))
- do while IniFileDiCtionary.Exists(AltBuf)
- AltBufAltBuf = 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
- ValSpCValSpC = 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
- StrBufStrBuf = StrBuf & "~"
- end if
- StrBufStrBuf = 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
- %>
若何利用?
看看这个:
Conn.asp
-----------------------------------------------
- <!--#inClude file="inifile.asp"-->
- <%
- 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"
- set Conn=Server.CreateObjeCt("adodb.ConneCtion")
- Connstr="PROVIDER=SQL语言法则OLEDB;DATA SOURCE="&dbip&";UID="&dbuid&";PWD="&dbpwd&";DATABASE="&dbname
- Conn.open Connstr
- 'response.write Conn
- response.write err.desCription
- %>
以上是“用ASP编程读INI配置文件的函数[网站编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |
评论内容只代表网友观点,与本站立场无关!
评论摘要(共 0 条,得分 0 分,平均 0 分)
查看完整评论