function to read INI configuration file with ASP

Source: Internet
Author: User

Requirements:

Ability to read the section and key of the INI file to read the corresponding value.

such as a configuration file

SMSVote.ini
---------------------------------
[SMSVote]
Server=(local)
DB=SMSVote
User=sa
PassWord=123
[DB2Vote]
Server=192.168.0.1
DB=DB2
User=sa
PassWord=
---------------------------------

Principal Program (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 = m  ID (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 ' are data on This is
if left (strbuf, 1) <> "then
' It ' is not a comment
if left (strbuf, 1) =" [Then
' It ' s a s Ection header
Hdrbuf = Mid (Strbuf, 2, Len (STRBUF)-2)
Else
' It ' s a value
StrPtr = InStr (strbuf, "=")

altbuf = LCase (Hdrbuf & "¦" & Left (Strbuf, StrPtr-1))
did while inifiledictionary.exists (ALTBUF)
Altbuf = Altbuf & "_"
Loop
Inifiledictionary.add altbuf, Mid (Strbuf, StrPtr + 1)
End If
End If
E nd if
loop
Inifil.close
Set inifil = Nothing
Set filsys = no
end Sub
Function Inifilevalue by Val valspc)
Dim ifarray
StrPtr = InStr (VALSPC, "¦")
VALSPC = LCase (VALSPC)
If strptr = 0 Then
' They W Ant the whole section
Strbuf = "
StrPtr = Len (valspc) + 1
valspc = valspc +" ¦ "
Ifarray = inifilediction ary. Keys
For i = 0 to Inifiledictionary.count-1
if left (Ifarray (i), strptr) = VALSPC then
' It ' from the SEC tion
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
%>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.