我好像看见过一个“读取INI文件”的函数,现在找不到了。

blackfire9 2005-10-29 08:53:27
我好像看见过一个“读取INI文件”的函数,现在找不到了。
那位有,麻烦提供一下,谢谢了。
...全文
196 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
laogou 2005-11-02
  • 打赏
  • 举报
回复
包括写入INI文件
<%
'Power By Tim
'文件摘要:INI类
'文件版本:3.0
'文本创建日期:2:17 2004-12-14
'================= 属性说明 ================
'INI.OpenFile = 文件路径(使用虚拟路径需在外部定义)
'INI.CodeSet = 编码设置,默认为 GB2312
'INI.IsTrue = 检测文件是否正常(存在)
'================ 方法说明 =================
'IsGroup(组名) 检测组是否存在
'IsNode(组名,节点名) 检测节点是否存在
'GetGroup(组名) 读取组信息
'CountGroup() 统计组数量
'ReadNode(组名,节点名) 读取节点数据
'WriteGroup(组名) 创建组
'WriteNode(组,节点,节点数据) 插入/更新节点数据
'DeleteGroup(组名) 删除组
'DeleteNode(组名,节点名) 删除节点
'Save() 保存文件
'Close() 清除内部数据(释放)
'===============================================


Class INI_Class
'===============================================
Private Stream '// Stream 对象
Private FilePath '// 文件路径
Public Content '// 文件数据
Public IsTrue '// 文件是否存在
Public IsAnsi '// 记录是否二进制
Public CodeSet '// 数据编码
'================================================

'// 初始化
Private Sub Class_Initialize()
Set Stream = Server.CreateObject("ADODB.Stream")
Stream.Mode = 3
Stream.Type = 2
CodeSet = "gb2312"
IsAnsi = True
IsTrue = True
End Sub


'// 二进制流转换为字符串
Private Function Bytes2bStr(bStr)
if Lenb(bStr)=0 Then
Bytes2bStr = ""
Exit Function
End if

Dim BytesStream,StringReturn
Set BytesStream = Server.CreateObject("ADODB.Stream")
With BytesStream
.Type = 2
.Open
.WriteText bStr
.Position = 0
.Charset = CodeSet
.Position = 2
StringReturn = .ReadText
.Close
End With
Bytes2bStr = StringReturn
Set BytesStream = Nothing
Set StringReturn = Nothing
End Function


'// 设置文件路径
Property Let OpenFile(INIFilePath)
FilePath = INIFilePath
Stream.Open
On Error Resume Next
Stream.LoadFromFile(FilePath)
'// 文件不存在时返回给 IsTrue
if Err.Number<>0 Then
IsTrue = False
Err.Clear
End if
Content = Stream.ReadText(Stream.Size)
if Not IsAnsi Then Content=Bytes2bStr(Content)
End Property


'// 检测组是否存在[参数:组名]
Public Function IsGroup(GroupName)
if Instr(Content,"["&GroupName&"]")>0 Then
IsGroup = True
Else
IsGroup = False
End if
End Function


'// 读取组信息[参数:组名]
Public Function GetGroup(GroupName)
Dim TempGroup
if Not IsGroup(GroupName) Then Exit Function
'// 开始寻找头部截取
TempGroup = Mid(Content,Instr(Content,"["&GroupName&"]"),Len(Content))
'// 剔除尾部
if Instr(TempGroup,VbCrlf&"[")>0 Then TempGroup=Left(TempGroup,Instr(TempGroup,VbCrlf&"[")-1)
if Right(TempGroup,1)<>Chr(10) Then TempGroup=TempGroup&VbCrlf
GetGroup = TempGroup
End Function


'// 检测节点是否存在[参数:组名,节点名]
Public Function IsNode(GroupName,NodeName)
if Instr(GetGroup(GroupName),NodeName&"=") Then
IsNode = True
Else
IsNode = False
End if
End Function


'// 创建组[参数:组名]
Public Sub WriteGroup(GroupName)
if Not IsGroup(GroupName) And GroupName<>"" Then
Content = Content & "[" & GroupName & "]" & VbCrlf
End if
End Sub


'// 读取节点数据[参数:组名,节点名]
Public Function ReadNode(GroupName,NodeName)
if Not IsNode(GroupName,NodeName) Then Exit Function
Dim TempContent
'// 取组信息
TempContent = GetGroup(GroupName)
'// 取当前节点数据
TempContent = Right(TempContent,Len(TempContent)-Instr(TempContent,NodeName&"=")+1)
TempContent = Replace(Left(TempContent,Instr(TempContent,VbCrlf)-1),NodeName&"=","")
ReadNode = ReplaceData(TempContent,0)
End Function


'// 写入节点数据[参数:组名,节点名,节点数据]
Public Sub WriteNode(GroupName,NodeName,NodeData)
'// 组不存在时写入组
if Not IsGroup(GroupName) Then WriteGroup(GroupName)

'// 寻找位置插入数据
'/// 获取组
Dim TempGroup : TempGroup = GetGroup(GroupName)

'/// 在组尾部追加
Dim NewGroup
if IsNode(GroupName,NodeName) Then
NewGroup = Replace(TempGroup,NodeName&"="&ReplaceData(ReadNode(GroupName,NodeName),1),NodeName&"="&ReplaceData(NodeData,1))
Else
NewGroup = TempGroup & NodeName & "=" & ReplaceData(NodeData,1) & VbCrlf
End if

Content = Replace(Content,TempGroup,NewGroup)
End Sub


'// 删除组[参数:组名]
Public Sub DeleteGroup(GroupName)
Content = Replace(Content,GetGroup(GroupName),"")
End Sub


'// 删除节点[参数:组名,节点名]
Public Sub DeleteNode(GroupName,NodeName)
Dim TempGroup
Dim NewGroup
TempGroup = GetGroup(GroupName)
NewGroup = Replace(TempGroup,NodeName&"="&ReadNode(GroupName,NodeName)&VbCrlf,"")
if Right(NewGroup,1)<>Chr(10) Then NewGroup = NewGroup&VbCrlf
Content = Replace(Content,TempGroup,NewGroup)
End Sub


'// 替换字符[实参:替换目标,数据流向方向]
' 字符转换[防止关键符号出错]
' [ ---> {(@)}
' ] ---> {(#)}
' = ---> {($)}
' 回车 ---> {(1310)}
Public Function ReplaceData(Data_Str,IsIn)
if IsIn Then
ReplaceData = Replace(Replace(Replace(Data_Str,"[","{(@)}"),"]","{(#)}"),"=","{($)}")
ReplaceData = Replace(ReplaceData,Chr(13)&Chr(10),"{(1310)}")
Else
ReplaceData = Replace(Replace(Replace(Data_Str,"{(@)}","["),"{(#)}","]"),"{($)}","=")
ReplaceData = Replace(ReplaceData,"{(1310)}",Chr(13)&Chr(10))
End if
End Function


'// 保存文件数据
Public Sub Save()
With Stream
.Close
.Open
.WriteText Content
.SaveToFile FilePath,2
End With
End Sub


'// 关闭、释放
Public Sub Close()
Set Stream = Nothing
Set Content = Nothing
End Sub

End Class


Dim INI
Set INI = New INI_Class
INI.OpenFile = Server.MapPath("Config.ini")
'========== 这是写入ini数据 ==========
INI.WriteNode("SiteConfig","SiteName","Leadbbs极速论坛")
INI.WriteNode("SiteConfig","Mail","leadbbs@leadbbs.com")
INI.Save()
'========== 这是读取ini数据 ==========
Response.Write("站点名称:"INI.ReadNode("SiteConfig","SiteName"))
%>
fantiny 2005-11-02
  • 打赏
  • 举报
回复
Sub WriteProfileString(section,key,filename,value)

If MyTrim(section)="" Or Mytrim(key)="" then
Exit Sub
End if

dim contentini
contentini=""

if fso.FileExists(filename) then
Dim readini,bsection,bSectionFound,bKeyFound
bKeyFound=False
bsection=False
bsectionFound=False

Set readini = fso.OpenTextFile(filename,1)
Do while not(readini.AtEndOfStream)
Dim strini,trimstrini
strini = readini.ReadLine
trimstrini = MyTrim(strini)
if Left(trimstrini,1)="[" and Right(Trimstrini,1)="]" then
if StrComp(Trimstrini,"[" & MyTrim(section) & "]",1)=0 Then
bsection=True
bsectionFound=True
else
bsection =False
end if
Else
if bsection then
Dim poskey
poskey = InStr(Trimstrini,"=")
if posKey>0 then
if StrComp(MyTrim(Left(Trimstrini,poskey-1)),MyTrim(key),1)=0 Then
bKeyFound = True
strini = Left(Trimstrini,poskey) & " " & value
end If
End if
end if
End if

if bsectionFound=True and bsection=False and bKeyFound=false then
contentini = contentini & key & "=" & value & vbcrlf
bKeyFound = True
end if

if MyTrim(strini)<>"" then
if Left(trimstrini,1)="[" and Right(Trimstrini,1)="]" And contentini<>"" then
contentini = contentini & vbCrlf
end if

contentini = contentini & strini & vbCrlf
end if
Loop

if bsectionFound=True and bsection=True and bKeyfound =false then
contentini = contentini & key & "=" & value & vbcrlf
end if

if bsectionFound=False Then
contentini = contentini & vbcrlf & "[" & section & "]" & vbcrlf & key & "=" & value & vbcrlf
end if

readini.Close

Else
contentini = "[" & section & "]" & vbcrlf & key & "=" & value & vbcrlf
End if


Dim writeini
set writeini = fso.CreateTextFile(filename,True)
writeini.Write contentini
writeini.Close
End Sub
blackfire9 2005-11-02
  • 打赏
  • 举报
回复
昏迷,结贴服务器出错,
帖子结了,分怎么没有给出去呢?
blackfire9 2005-11-02
  • 打赏
  • 举报
回复
非常感谢 老狼 提供的这个类,正是我想要的。
也谢谢 fantiny 虽然我看不懂 “MyTrim” 是什么(是个内置函数吗?)
谢谢大家了
blackfire9 2005-11-01
  • 打赏
  • 举报
回复
非常感谢 beiouwolf ,
我试试。。
beiouwolf 2005-11-01
  • 打赏
  • 举报
回复
前面漏写文件名了~~~

Function GetValue(iniName,Group,Item)
dim Fso
dim TF
dim ThisLine
dim SplitLine
set Fso = server.createobject("scripting.filesystemobject")
set TF = Fso.opentextfile(iniName)

do while not TF.atendofstream
ThisLine = TF.readline
if trim(ThisLine) <> "" then '判断空行
Temp = "[" & Group & "]"
if left(ThisLine,len(Group)+2) = Temp then
ThisLine = TF.readline
do while not (left(ThisLine,1) = "[" and TF.atendofstream)
'if (asc(left(ThisLine,1)) > 65 and asc(left(ThisLine,1)) < 90) and (asc(left(ThisLine,1)) > 97 and asc(left(ThisLine,1)) < 122) then '判断第一个字符是不是字母
SplitLine = split(ThisLine,"=")
if SplitLine(0) = Item then
GetValue = SplitLine(1)
exit do
'end if
end if
ThisLine = TF.readline
loop
end if
end if
loop

end Function
beiouwolf 2005-11-01
  • 打赏
  • 举报
回复
Function GetValue(Group,Item)
dim Fso
dim TF
dim ThisLine
dim SplitLine
set Fso = server.createobject("scripting.filesystemobject")
set TF = Fso.opentextfile(server.mappath("1.ini"))

do while not TF.atendofstream
ThisLine = TF.readline
if trim(ThisLine) <> "" then '判断空行
Temp = "[" & Group & "]"
if left(ThisLine,len(Group)+2) = Temp then
ThisLine = TF.readline
do while not (left(ThisLine,1) = "[" and TF.atendofstream)
'if (asc(left(ThisLine,1)) > 65 and asc(left(ThisLine,1)) < 90) and (asc(left(ThisLine,1)) > 97 and asc(left(ThisLine,1)) < 122) then '判断第一个字符是不是字母
SplitLine = split(ThisLine,"=")
if SplitLine(0) = Item then
GetValue = SplitLine(1)
exit do
'end if
end if
ThisLine = TF.readline
loop
end if
end if
loop

end Function
blackfire9 2005-10-31
  • 打赏
  • 举报
回复
谢谢pilicat,我需要的是一个读取INI文件的函数,要 VBScript 来实。不是操作注册表的
pilicat 2005-10-30
  • 打赏
  • 举报
回复
请参阅:

http://www.pconline.com.cn/pcedu/empolder/wz/vbscript/0507/667539.html


---------------------------------------------------
http://kmok.cn
pilicat 2005-10-30
  • 打赏
  • 举报
回复
需要用VBScript 来实现吗?


---------------------------------------------------
http://kmok.cn
blackfire9 2005-10-30
  • 打赏
  • 举报
回复
谢谢 beiouwolf
就是需要一个函数,能够通过传递“INI文件名”、“方括号里的小节名”、“条目名”等参数,来读取相应的值。例如要返回下面例子里的“Mail”小节里“MAPI”的值。

[fonts]
[extensions]
[mci extensions]
[files]
[Mail]
MAPI=1
[MCI Extensions.BAK]
aif=MPEGVideo
aifc=MPEGVideo
beiouwolf 2005-10-30
  • 打赏
  • 举报
回复
你需要什么样的,我可以帮你写一个
moodboy1982 2005-10-29
  • 打赏
  • 举报
回复
没见过。

4,008

社区成员

发帖
与我相关
我的任务
社区描述
它是一种微软环境下的轻量级的解释型语言,它使用COM组件、WMI、WSH、ADSI访问系统中的元素,对系统进行管理。
社区管理员
  • vbScript社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧