function to read INI configuration file with ASP

Source: Internet
Author: User

Choose a Blog from mind_1220

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 = 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 as not Inifil.atendofstream
Strbuf = Inifil.readline
If Strbuf <> "" Then
' There is Data '
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)
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 are from ' 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
%>

How to use it?

Check this out:
Conn.asp
-----------------------------------------------
<!--#include file= "inifile.asp"-->

<%
On Error Resume Next
Dim Conn,connstr,dbuid,dbpwd,dbname,dbip
Name of the call Inifileload ("Virtual=smsvote.ini") ' Config file
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=sqloledb;data source=" "&dbip&"; Uid= "&dbuid&"; Pwd= "&dbpwd&";D atabase= "&dbname
Conn.Open ConnStr
' Response.Write Conn
Response.Write Err.Description
%>



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.