'修改子节点的值
newVar.childNodes.item(0).nodeTypedValue = varname
newVar.childNodes.item(1).nodeTypedValue = varvalue
'添加节点
Set newEnv= Environ.appendChild(newVar)
'保存xml
objxml.save filepath
End Function
Function updateVar(filepath,varname,varvalue) '已完成
Set objxml= CreateObject("MSXML.DOMDocument")
objxml.load filepath
Set Environ = objxml.lastChild '获得
allVar = Environ.childNodes.length '获得
' Set dic = CreateObject("Scripting.Dictionary")
' For i=0 To allVar-1
' Set vars=Environ.childNodes.item(i) '获得
' var_name=vars.childNodes.item(0).nodeTypedValue
' var_value=vars.childNodes.item(1).nodeTypedValue
' dic.Add var_name,var_value
' Next
' If Not dic.Exists(varname) Then
' addVar filepath,varname,varvalue
' Exit Function
' End If
For i=0 To allVar-1
Set vars=Environ.childNodes.item(i) '获得
var_name=vars.childNodes.item(0).nodeTypedValue
If var_name=varname Then
vars.childNodes.item(1).nodeTypedValue = varvalue '更新变量值
Exit For
End If
Next
'保存xml
objxml.save filepath
End Function
Function delVar(filepath,varname) '已完成
Set objxml= CreateObject("MSXML.DOMDocument")
objxml.load filepath
Set Environ = objxml.lastChild '获得
allVar = Environ.childNodes.length '获得
If allVar=1 Then
Exit Function
End If
For i=0 To allVar-1
Set vars=Environ.childNodes.item(i) '获得
var_name=vars.childNodes.item(0).nodeTypedValue
If var_name=varname Then
Environ.removeChild(vars) '删除节点
Exit For
End If
Next
'保存xml
objxml.save filepath
End Function
filepath="C:/UserDedinedEnvironment.xml"
Set dics = GetVars(filepath)
addVar filepath,"tttt","aaaa"
Set dics = GetVars(filepath)
updateVar filepath,"tttt","55555555"
Set dics = GetVars(filepath)
delVar filepath,"tttt"
Set dics = GetVars(filepath)
xx=1
Function GetVars(filepath) '已完成
Set objxml= CreateObject("MSXML.DOMDocument")
objxml.load filepath
Set Environ = objxml.lastChild '获得
Set dic = CreateObject("Scripting.Dictionary")
allVar = Environ.childNodes.length '获得
For i=0 To allVar-1
Set vars=Environ.childNodes.item(i) '获得
var_name=vars.childNodes.item(0).nodeTypedValue
var_value=vars.childNodes.item(1).nodeTypedValue
dic.Add var_name,var_value
Next
Set GetVars = dic '返回dictionary的环境变量列表
Set dic = Nothing
End Function
Function addVar(filepath,varname,varvalue) '已完成
Set objxml= CreateObject("MSXML.DOMDocument")
objxml.load filepath
Set Environ = objxml.lastChild '获得
Set dic = CreateObject("Scripting.Dictionary")
allVar = Environ.childNodes.length '获得
For i=0 To allVar-1
Set vars=Environ.childNodes.item(i) '获得
var_name=vars.childNodes.item(0).nodeTypedValue
var_value=vars.childNodes.item(1).nodeTypedValue
dic.Add var_name,var_value
Next
If dic.Exists(varname) Then
updateVar filepath,varname,varvalue
Exit Function
End If