如果你想从一个INI读取键/值对文件,你会更好,将它们存储在字典中。几年前我写了一篇function。基本上看起来像这样:
Function ParseIni(filename)
Set ParseIni = Nothing
Set config = CreateObject("Scripting.Dictionary")
section = ""
Set file = CreateObject("Scripting.FileSystemObject").OpenTextFile(filename)
Do While Not file.AtEndOfStream
line = Trim(Replace(file.ReadLine, vbTab, " "))
If InStr(line, ";") > 0 Then line = Trim(Left(line, InStr(line, ";") - 1))
If line <> "" Then
If Left(line, 1) = "[" And Right(line, 1) = "]" Then
' line is a section name
section = Trim(Mid(line, 2, Len(line) - 2))
If section = "" Then _
WScript.Echo "Parse Error: section name is empty string."
If config.Exists(section) Then _
WScript.Echo "Parse Error: duplicate section name '" & name & "'."
config.Add section, CreateObject("Scripting.Dictionary")
ElseIf InStr(line, "=") > 0 Then
' line is a parameter line
If section = "" And Not config.Exists(section) Then _
config.Add section, CreateObject("Scripting.Dictionary")
param = Split(line, "=", 2)
param(0) = Trim(param(0))
param(1) = Trim(param(1))
If param(0) = "" Then _
WScript.Echo "Parse Error: invalid parameter name '" & param(0) & "'."
If param(1) = "" Then param(1) = True
config(section).Add param(0), param(1)
Else
' line is neither parameter nor section name, thus invalid
WScript.Echo "Parse Error: expected parameter definition in line '" _
& line & "'."
End If
End If
Loop
file.Close
Set ParseIni = config
End Function
我认为你需要重新评估你的架构。有一个ini文件,可以设置任何变量,它想要的声音...不安全。 – Richard
在我的例子中看起来不像它的实际工作方式。没有风险。 – Dss
'eval'和风险是同义词。 –