用ASP读INI配置文件的函数

选择自 mind_1220 的 Blog

要求:
能够读取按照 INI文件的Section和Key来读出相应的Value。
比如一个配置文件
SMSVote.ini
---------------------------------
[SMSVote]
Server=(local)
DB=SMSVote
User=sa
PassWord=123
[DB2Vote]
Server=192.168.0.1
DB=DB2
User=sa
PassWord=
---------------------------------

主体程序(方法) :

inifile.asp
-----------------------------------------------

 1   
 2set IniFileDictionary = CreateObject("Scripting.Dictionary")   
 3  
 4Sub IniFileLoad(ByVal FilSpc)   
 5IniFileDictionary.RemoveAll   
 6FilSpc = lcase(FilSpc)   
 7if left(FilSpc, 1) = "p" then   
 8'Physical path   
 9PhyPth = mid(FilSpc, instr(FilSpc, "=") + 1)   
10else   
11'Virtual path   
12PhyPth = Server.MapPath(mid(FilSpc, instr(FilSpc, "=") + 1))   
13end if   
14  
15set FilSys = CreateObject("Scripting.FileSystemObject")   
16set IniFil = FilSys.OpenTextFile(PhyPth, 1)   
17do while not IniFil.AtEndOfStream   
18StrBuf = IniFil.ReadLine   
19if StrBuf <> "" then   
20'There is data on this line   
21if left(StrBuf, 1) <> ";" then   
22'It's not a comment   
23if left(StrBuf, 1) = "[" then   
24'It's a section header   
25HdrBuf = mid(StrBuf, 2, len(StrBuf) - 2)   
26else   
27'It's a value   
28StrPtr = instr(StrBuf, "=")   
29AltBuf = lcase(HdrBuf & " ¦" & left(StrBuf, StrPtr - 1))   
30do while IniFileDictionary.Exists(AltBuf)   
31AltBuf = AltBuf & "_"   
32loop   
33IniFileDictionary.Add AltBuf, mid(StrBuf, StrPtr + 1)   
34end if   
35end if   
36end if   
37loop   
38IniFil.Close   
39set IniFil = nothing   
40set FilSys = nothing   
41End Sub   
42  
43Function IniFileValue(ByVal ValSpc)   
44dim ifarray   
45StrPtr = instr(ValSpc, " ¦")   
46ValSpc = lcase(ValSpc)   
47if StrPtr = 0 then   
48'They want the whole section   
49StrBuf = ""   
50StrPtr = len(ValSpc) + 1   
51ValSpc = ValSpc + " ¦"   
52ifarray = IniFileDictionary.Keys   
53for i = 0 to IniFileDictionary.Count - 1   
54if left(ifarray(i), StrPtr) = ValSpc then   
55'This is from the section   
56if StrBuf <> "" then   
57StrBuf = StrBuf & "~"   
58end if   
59StrBuf = StrBuf & ifarray(i) & "=" & IniFileDictionary(ifarray(i))   
60end if   
61next   
62else   
63'They want a specific value   
64StrBuf = IniFileDictionary(ValSpc)   
65end if   
66IniFileValue = StrBuf   
67End Function   
68Function Chr(section,key)   
69char1=IniFileValue(section)   
70SearchString =char1   
71SearchChar = key   
72MyPos=Instr(1,SearchString,SearchChar,1)   
73'char2=section+key   
74char1=mid(char1,MyPos+len(key)+1,len(char1)-MyPos+1)   
75SearchString =char1   
76SearchChar = "~"   
77MyPos=Instr(1,SearchString,SearchChar,1)   
78if MyPos<>0 then   
79char1=mid(char1,1,MyPos-1)   
80else   
81char1=mid(char1,1)   
82end if   
83Chr = char1   
84End Function   

如何使用?

看看这个:
conn.asp
-----------------------------------------------

 1   
 2on error resume next   
 3dim conn,connstr,dbuid,dbpwd,dbname,dbip   
 4call IniFileLoad("virtual=SMSVote.ini") '配置文件的名字   
 5dbuid=Chr("SMSVote","User") 'Section="SMSVote",Key="User"   
 6dbpwd=Chr("SMSVote","PassWord") 'Section="SMSVote",Key="PassWord"   
 7dbname=Chr("SMSVote","DB") 'Section="SMSVote",Key="DB"   
 8dbip=Chr("SMSVote","server") 'Section="SMSVote",Key="server"   
 9  
10set conn=Server.CreateObject("adodb.Connection")   
11connstr="PROVIDER=SQLOLEDB;DATA SOURCE="&dbip&";UID="&dbuid&";PWD="&dbpwd&";DATABASE="&dbname   
12conn.open connstr   
13'response.write conn   
14response.write err.description   
Published At
Categories with Web编程
Tagged with
comments powered by Disqus