選擇自 mind_1220 的部落格
需求:
能夠依照INI檔案的Section和Key來讀取相應的Value。
比如說一個設定檔
簡訊投票.ini
---------------------------------
[簡訊投票]
伺服器=(本地)
DB=簡訊投票
使用者=sa
密碼=123
[DB2投票]
伺服器=192.168.0.1
DB=DB2
使用者=sa
密碼=
---------------------------------
主體程序(方法):
ini文件
-------------------------------------------------- ---------
<%
設定 IniFileDictionary = CreateObject("Scripting.Dictionary")
子 IniFileLoad(ByVal FilSpc)
IniFileDictionary.RemoveAll
FilSpc = lcase(FilSpc)
如果 left(FilSpc, 1) = "p" 那麼
'物理路徑
PhyPth = mid(FilSpc, instr(FilSpc, "=") + 1)
別的
'虛擬路徑
PhyPth = Server.MapPath(mid(FilSpc, instr(FilSpc, "=") + 1))
結束如果
設定 FilSys = CreateObject("Scripting.FileSystemObject")
設定 IniFil = FilSys.OpenTextFile(PhyPth, 1)
在不 IniFil.AtEndOfStream 時執行
StrBuf = IniFil.ReadLine
如果 StrBuf <> "" 那麼
'這條線上有數據
if left(StrBuf, 1) <> ";" 然後
'這不是評論
如果左(StrBuf,1)=“[”那麼
'這是一個節標題
HdrBuf = mid(StrBuf, 2, len(StrBuf) - 2)
別的
'這是一個值
StrPtr = instr(StrBuf, "=")
AltBuf = lcase(HdrBuf & " " & left(StrBuf, StrPtr - 1))
while IniFileDictionary.Exists(AltBuf) 執行
AltBuf = AltBuf & "_"
環形
IniFileDictionary.Add AltBuf, mid(StrBuf, StrPtr + 1)
結束如果
結束如果
結束如果
環形
初始關閉
設定 IniFil = 無
設定 FilSys = 無
結束子
函數 IniFileValue(ByVal ValSpc)
昏暗的 ifarray
StrPtr = instr(ValSpc, " ψ")
ValSpc = lcase(ValSpc)
如果 StrPtr = 0 那麼
「他們想要整個部分
StrBuf=“”
StrPtr = len(ValSpc) + 1
ValSpc = ValSpc + " "
ifarray = IniFileDictionary.Keys
對於 i = 0 到 IniFileDictionary.Count - 1
如果 left(ifarray(i), StrPtr) = ValSpc 那麼
'這是來自該部分
如果 StrBuf <> "" 那麼
StrBuf = StrBuf & “~”
結束如果
StrBuf = StrBuf & ifarray(i) & "= & IniFileDictionary(ifarray(i))
結束如果
下一個
別的
「他們想要一個特定的價值
StrBuf = IniFileDictionary(ValSpc)
結束如果
IniFileValue = StrBuf
結束功能
函數 Chr(節,鍵)
char1=IniFileValue(節)
搜尋字串=char1
搜尋字元 = 鍵
MyPos=Instr(1,搜尋字串,搜尋字元,1)
'char2=節+鍵
char1=mid(char1,MyPos+len(key)+1,len(char1)-MyPos+1)
搜尋字串=char1
搜尋字元=“~”
MyPos=Instr(1,搜尋字串,搜尋字元,1)
如果 MyPos<>0 那麼
char1=mid(char1,1,MyPos-1)
別的
字元1=中(字元1,1)
結束如果
Chr = 字元1
結束功能
%>
如何使用?
看看這個:
康乃狄克州
-------------------------------------------------- ---------
<!--#include file="inifile.asp"-->
<%
出錯時繼續下一步
暗淡 conn、connstr、dbuid、dbpwd、dbname、dbip
call IniFileLoad("virtual=SMSVote.ini") '設定檔的名稱
dbuid=Chr("SMSVote","用戶") 'Section="SMSVote",Key="用戶"
dbpwd=Chr("SMSVote","PassWord") 'Section="SMSVote",Key="PassWord"
dbname=Chr("SMSVote","DB") 'Section="SMSVote",Key="DB"
dbip=Chr("SMSVote","伺服器") 'Section="SMSVote",Key="伺服器"
設定 conn=Server.CreateObject("adodb.Connection")
connstr="PROVIDER=SQLOLEDB;DATA SOURCE="&dbip&";UID="&dbuid&";PWD="&dbpwd&";DATABASE="&dbname
conn.打開 connstr
'response.write conn 'response.write conn
響應.寫錯誤.描述
%>